// infinite loop to read and handle a line of data
for (;;) {
infile >> firstThingOnTheLine;
if (infile.eof()) break;
// read the rest of the line
// do whatever with data
}
If you dislike infinite loops, you can accomplish this same thing
using a while loop by priming the loop and reading again at the end:
infile >> firstThingOnTheLine;
while (!infile.eof()) {
// read the rest of the line
// do whatever with data
infile >> firstThingOnTheLine;
}
You MUST do it one of these ways. Do not adjust your code.
Adjust the data file to work with this code.
Often reading is done in a routine. In that case, exit both when eof.
This loop is similar to that situation.
ObjClass obj;
for (;;) {
obj.setData(infile); // method does reading
if (infile.eof()) break;
// do whatever with obj
}
void ObjClass::setData(istream& infile) {
infile >> firstThingOnTheLine; // so EOF char is read
if (infile.eof()) return; // determine no more data
// probably read more data to fill rest of object
}