istream object a get() delimiter.

Hey all! I'm trying to write a program that will read in the file one char at a time and take an action based on the info. How you use the delimiter argument of the dot get() function. I've been testing a few things and can seem to get it to work.

The first code is the main program I'm writing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 cout << "Please enter the file name to by read: ";
	string readFile;
	getline(cin, readFile);
	inFile.open(readFile);
	if (inFile.is_open())
	{
		char charBuf; // To hold each character read by inFile stream
		string strBuf; // To collect past characters read
		string numBuf; // To collect past numbers read
		array<string,5> keyword = {"if", "then", "else", "begin", "end"};
		array<char,9> special = { '(', ')','[',']','+','-','=',',',';' };
		while (inFile.get(charBuf))
		{
			// What if inFile reads a delimiter i.e \n \t or space
			// Check if inFile reads a special character
			for (auto spchar = special.begin(); spchar != special.end(); spchar++)
			{
				if (charBuf == *spchar)
				{
					if (strBuf.length > 0)
					{

					}
				}
			}
			if (isalpha(charBuf))
			{
				numBuf.clear(); // Clearing Num Buffer because there is no digit character in syntax
				strBuf = charBuf;
				for (auto key = keyword.begin(); key != keyword.end(); key++) //Checking collection of characters against keywords
				{
					if (strBuf == *key)
					{
						for (map<string, Token*>::iterator map = tokenMap.begin(); map != tokenMap.end(); ++map) // Checking if the keyword is already in the map
						{
							if (map->first == strBuf)
							{
								map->second->plusCount(); //increment count variable for evertime the keyword is found in file
								strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
							}
							else
							{
								Token * newToken = new Token("keyword", 1); //DONT FORGET TO DELETE!
								tokenMap.insert(pair<string, Token*>(strBuf, newToken)); //Not found in the map so create a new token and store pointer in map
								strBuf.clear(); // If a keyword was found clear strBuf to make room for next token.
							}
						}
					}
				}
				
			}


While this is the test I made for the dot get()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
	string myString = "myTest.txt";
	ifstream myStream;
	char buffer;
	char delim = '\n';

	myStream.open(myString);
	if (myStream.is_open())
	{
		while (myStream.get(buffer, delim)) //Won't accept delim as arg
		{
			if (buffer == '/n')
				cout << "NEWLINE" << endl;
			cout << buffer;
		}
	}
	else
		cout << "File did not open" << endl;

    return 0;
}
How you use the delimiter argument of the dot get() function.

Did you try reading some documentation for this standard function?
http://www.cplusplus.com/reference/istream/istream/get/

Since you're trying to use this function with a single character there is no version of the function that takes more than one argument. You have two versions of the function available, one that takes no arguments and one that takes one argument (a char).

single character (1) int get();
istream& get (char& c);
I did see that, I guess I'm asking if there is a way to track if the stream reads one of the delimiters that I need. For example is the stream reads a newline, I will need to take an action but I'm not sure how to stop the loop for that condition.
I will need to take an action but I'm not sure how to stop the loop for that condition.

What? You're already testing for at least one of your "conditions", look at line 13 of the second snippet.

Is there a reason you're trying to read the file character by character?

OP: try std::noskipws http://www.cplusplus.com/reference/ios/noskipws/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>

int main()
{
    std::ifstream inFile("C:\\input.txt");
    char ch;

    while (inFile >> std::noskipws >> ch)
    {
        if(ch == ' ')
            std::cout << "blank\n";
        if (ch == '\n')
            std::cout << "newline\n";
        //or do something else with ch
    }
}
By default retrieving a char doesn't skip whitespace when using the extraction operator, so using noskipws is unnecessary.

I'm reading the file one character at a time because I need to sort the input from the file into tokens. These tokens have been pre-established. One possibility is an "character" token, which can be a character, or a character followed by a character. I also need to account for special characters like "(" and "]". So if i see something like "adc(123)", I have to be able to separate the "abc" as one token, the "(" as another token, "123" and finally ")". Making entries into a map to record how many times each token appears.

Gunnerfunner: I did line 13 to see if the get() function would return the newline, but it doesn't, at least not in the way that I'm testing for it. The sample file that I'm using has two newlines but my test program never calls out "NEWLINE".

My sample file is very simple:

if(then)
123
abc

So whenever the stream comes across a delimiter such as a "space", "special character" like the "(" in the sample file, a "tab", or a "newline", I need to be able to check the past characters saved in my string strBuffer variable against the valid tokens to either make a new entry into the map or increment a count.
Last edited on
Gunnerfunner: I did line 13 to see if the get() function would return the newline, but its doesn't. the sample file that I'm using has two newlines but my test program never calls out "NEWLINE".

A newline is '\n', not '/n'.
By default retrieving a char doesn't skip whitespace when using the extraction operator, so using noskipws is unnecessary.


Sample file:
Mike Smith 858-343-2009
John Jackson 617-255-0987

above program output reading sample file with std::noskipws:
1
2
3
4
5
6
7
8
blank
blank
newline
blank
blank

Process returned 0 (0x0)   execution time : 0.218 s
Press any key to continue.


above program output reading sample file without std::noskipws:
1
2
3

Process returned 0 (0x0)   execution time : 0.203 s
Press any key to continue.



above program output reading sample file with std::noskipws:

The OP was using std::istream::get, which is unaffected by std::noskipws, so it doesn't really have any bearing on the code or behavior in the OP.

I'm sure jlb meant to say get rather than extraction operator as it clearly does affect the latter.
You are right cire, as I just tested it.

Gunnerfunner: you are also right though I should have looked at my code with a little more care because after using the right newline ie '\n' and not '/n' for the comparison, the get() function clearly reads the newline.
Now that I cam able to see the newlines and tabs I need a way to see the a space. Any ideas for this?
if you can help privately. plz cintact. thanks
Last edited on
I'm sure jlb meant to say get rather than extraction operator as it clearly does affect the latter.

yes, that would be correct
I need a way to see the a space. Any ideas for this?


if(ch == ' ')
Last edited on

its compiling right but the computation of the formula is not working? why? plz help? coulumb_potential=1.0/4*((particle_charge*e)/pi*E*distance);

Maybe make a new thread, rather than hijacking someone else's thread. Be sure to use code tags to make your code easier to read (which means people would be more likely to help).
http://www.cplusplus.com/articles/jEywvCM9/
Thanks again Gunner.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
	string myString = "myTest.txt";
	ifstream myStream;
	char buffer;
	char delim = '\n';

	myStream.open(myString);
	if (myStream.is_open())
	{
		while (myStream.get(buffer))
		{
			if (buffer == '\n')
				cout << "NEWLINE";
			if (buffer == ' ')
				cout << "SPACE";
			if (buffer == '\t')
				cout << "TAB";
			cout << buffer;


The output gives me exactly what I need. I also found the isspace() function, a member of cctype. Is there any reason why I shouldn't use that? Besides it might that be doing to much and going overboard?
Last edited on
isspace(buffer):
SPACESPACENEWLINESPACESPACESPACE
buffer == ' ':
SPACESPACENEWLINESPACESPACESPACE

it's better to use the library functions wherever possible

edit: btw OP you have an unused variable, char delim, in your program or at least within the code that you posted last
Last edited on
Fair enough, Thank a lot for your help friend!
Topic archived. No new replies allowed.