Whitespace at end of file causing EOF check to fail in C?

Presuming iFile is an istream : You should break out of the loop on any error, not only on EOF (which can be checked for with iFile.eof() BTW), because this is an endless loop when any format failure sets the stream into a bad state other that EOF. It is usually necessary to break out of a reading loop in the middle of the loop, after everything was read (either successfully or not), and before it is entered To make sure there isn't anything interesting coming anymore, you could, after the loop, reset the stream state and then try to read whitespace only until your reach EOF: while(!iFile.eof() ) { iFile >> std::ws; string line; std::getline(iFile,line); if(!line.empty()) error(...); }.

Presuming iFile is an istream: You should break out of the loop on any error, not only on EOF (which can be checked for with iFile.eof(), BTW), because this is an endless loop when any format failure sets the stream into a bad state other that EOF. It is usually necessary to break out of a reading loop in the middle of the loop, after everything was read (either successfully or not), and before it is entered. To make sure there isn't anything interesting coming anymore, you could, after the loop, reset the stream state and then try to read whitespace only until your reach EOF: while(!iFile.eof() ) { iFile >> std::ws; string line; std::getline(iFile,line); if(!line.empty()) error(...); }.

EOF is not a prediction but an error state. Hence, you can't use it like you're using it now, to predict whether you can read Column 1, 2 and 3. For that reason, a common pattern in C++ is: while (input >> obj1 >> obj2) { use(obj1, obj2); } All operator>>(istream& is, T&) return the inputstream, and when used in boolean context the stream is "true" as long as the last extraction succeeded.It's then safe to use the extracted objects.

If any of the reads fail (where you read the column data), just break out of the while loop. Presumably you are then at the end of the file and reading the last 'not correct' line.

– blcArmadillo Oct 27 '09 at 8:06 the EOF will be set. But it might be easier to just go line by line, and parse the values out of the line, instead of doing 3 consecutive reads – Toad Oct 27 '09 at 8:24.

Maybe you'll consider it a good idea to handle whitespace and other invalid input then. Perhaps some basic validation of columns 1,2,3 would be desirable as well.

Don't worry about the number of times that you loop: just validate your data and handle invalid inputs. Basically, check that you have three columns to read and if you don't decide if it's because the file is over or because of some other issue.

EOF is not a prediction but an error state. Hence, you can't use it like you're using it now, to predict whether you can read Column 1, 2 and 3. All operator>>(istream& is, T&) return the inputstream, and when used in boolean context the stream is "true" as long as the last extraction succeeded.

It's then safe to use the extracted objects.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions