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()
?
It skips last value now. Why is this happening and what's the difference between
fileIn
andfileIn.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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With