Thursday, October 25, 2018

c++ - Why am I reading the last word in my file twice?

I'm trying to read in the text from a file and this is what I am doing:



int main(int argc, char* argv[])
{

ifstream inFile;
inFile.open(argv[1]);


string item;

while(inFile.good())
{
inFile >> item;
cout << item << " " << endl;
}


For some reason it will read the last word in the file twice. I tried using a count variable to keep track of how many times it enters the while loop and it always enters one time more then the total number of line in the file. I think this is happening because the inFile.good() statement is not returning false soon enough. How can if fix it?

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...