Difference between ifstream.good() and bool(ifstream)

Szymon Bednorz picture Szymon Bednorz · Jan 29, 2017 · Viewed 8.8k times · Source

I'm writing a program which takes several variables from a text file. When the program finds EOF,
it ends entering data.

int main()
{
    int val, count = 0;
    ifstream fileIn;
    fileIn.open("num.txt");

    fileIn >> val;
    while (fileIn)
    {
        ++count;
        cout << "number: " << val << endl;
        fileIn >> val;
    }
    cout << "count: " << count << endl;

    fileIn.close();
    return 0;
}

num.txt file: 11 22 33 44

Program output:

number: 11
number: 22
number: 33
number: 44
count: 4

Everything is OK. But if I change the while condition section from fileIn to fileIn.good(),
the program output will look like this:

number: 11
number: 22
number: 33
count: 3

It skips last value now. Why is this happening and what's the difference between fileIn
and fileIn.good()?

Answer

WhiZTiM picture WhiZTiM · Jan 29, 2017

It skips last value now. Why is this happening and what's the difference between fileIn and fileIn.good()?

fileIn >> val;
while (fileIn)
{
    ++count;
    cout << "number: " << val << endl;
    fileIn >> val;
}

For a given content of "11 22 33 44" in the stream associated with fileIn. Using the bool(fileIn) conversion operator will return whether the stream has not failed. Note that reaching eof() isn't failure of the stream. Failure is basically when an I/O operation fails.

So, after reading the last number 44 into val. The loop is entered again because the stream has not failed: count is incremented, val is printed, but the next fileIn >> val will fail. Which will then be tested in the while loop's condition, and the failure will cause the loop to exit.

Doing fileIn.good() returns false if any of the stream's state flags have been set. Specifically, eof, fail and bad