// 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 on windows/mac to work with this code.
To do that, when the cursor is at the end of the last line of data,
press return. (Don't do this on linux.) The best way to create the datafile
on linux is by using pico, e.g., pico data3.txt, then copy and paste into
data3.txt.
Often reading is done in a routine. In that case, exit both when eof.
This loop is similar to that situation.
ObjClass obj;
// wherever reading datafile is done
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
}