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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.