Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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()?

like image 366
Szymon Bednorz Avatar asked Jan 29 '17 21:01

Szymon Bednorz


2 Answers

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

like image 162
WhiZTiM Avatar answered Oct 16 '22 04:10

WhiZTiM


if (fileIn) is equivalent to if (!fileIn.fail()), which returns true if badbit or failbit is set in rdstate().

On the other hand fileIn.good() checks if rdstate() is equal to 0. So in addition to fail() it also checks eofbit flag of.

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

Because you read val then check condition of stream using while(fileIn.good()) and then output last value, which will never be seen because then fileIn has eofbit set. Which is not checked by if(infs) as stated above.

like image 44
marcinj Avatar answered Oct 16 '22 04:10

marcinj