Using eof() in a while loop like this is wrong:
1 2 3 4 5
|
while (!fileIn.eof())
{
fileIn >> num;
// etc.
}
|
The problem is, the
fileIn >>
at line 3 might fail for many different reasons, just one of which is eof. But more importantly, the program goes on to use the result without bothering to check whether the read was successful or not. By the time it gets to check eof() in the while condition, it is already too late.
In the original program, this can lead to processing the last value twice, giving incorrect results, depending upon whether or not there was a newline or any whitespace after the last value in the file.
This is a correct way to handle it:
1 2 3 4 5 6
|
while (fileIn >> num)
{
cout << num << " ";
total += num;
count++;
}
|
Notice there is no eof() test here - it is simply not needed, and is best avoided as mentioned above. How does this work? Well, inside the while condition, this
fileIn >> num
attempts to read a number from the file. If a number was successfully read in, the result tests as
true
and the body of the loop is executed. If it fails for any reason, say because there is non-numeric data in the file, or because the end of file has been reached, it gives
false
.
Now on to the other parts of your question. In order to deal with the data on each row separately, the easiest way is to read the entire row into a string, then process the contents of the string. Again, this is made incredibly easy by using a stringstream, which allows a std::string to be treated as if it was a file containing data. A simple example:
1 2
|
#include <string>
#include <sstream>
|
1 2 3 4 5 6 7 8 9 10
|
string line;
while (getline(fileIn, line))
{
istringstream ss(line);
while (ss >> num)
{
cout << num << " ";
}
cout << endl;
}
|