0

Hello all,
I'm trying to parse a file, but for some reason, getline is combining my two letter words with other words.

Here is the portion of code I'm working with: note: my program will always ignore the first word in a line

char line[256];
string word;  
char *line2=new char(); 
	fgets(line, 256, in); //get the line from the in stream (which contains the file)
	if (feof(in))
	 return;
	printf("%s",line); //print first line
	strncpy(line2,line,(strlen(line)-1)); //copy the line into a char pointer, minus the newline char
	string str;
str.assign(line2); //copy the line2 char* into a string, this maybe be redundent

stringstream s(str); //set s as a sstream reading from the str
getline(s, word, ' '); //gets the first word, which I do not use
while (getline(s, word, ' ')){ //gets the following words in the line in order and parses
cout<<"word is " << word << endl;

//pseudo code here where if the current parsed word is recognized, then the program prints something, otherwise it will recurse to the next line, not really important here
}

For example, say in my file I have
pet CAT AB

When I use my cout to check my parsed words, I would see CAT, then ABCAT. Any reason why? Thanks.

Edited by Deadmon: n/a

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by sfuo
0

Have you tried flushing the stream? I don't use fgets() so I'm not 100% sure if that will work.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.