while(true)
{
int a, c;
string b;
file >> a >> b >> c;
if( file.good() )
f(a, b, c);`
else
break;
}
This code is not reading the last line form .txt file. If I change file.good()
to !file.fail()
it works. Why?
while(true)
{
int a, c;
string b;
file >> a >> b >> c;
if( file.good() )
f(a, b, c);`
else
break;
}
This code is not reading the last line form .txt file. If I change file.good()
to !file.fail()
it works. Why?
bad() --> Returns true if a reading or writing operation fails. For example, in the case that we try to write to a file that is not open for writing or if the device where we try to write has no space left.
fail() --> Returns true in the same cases as bad(), but also in the case that a format error happens, like when an alphabetical character is extracted when we are trying to read an integer number.
good() --> It is the most generic state flag: it returns false in the same cases in which calling any of the previous functions would return true. Note that good and bad are not exact opposites (good checks more state flags at once).
Will elaborate latter.
I think this is covered here.
A relevant excerpt: "All of the stream state functions – fail, bad, eof, and good – tell you the current state of the stream rather than predicting the success of a future operation. Check the stream itself (which is equivalent to an inverted fail check) after the desired operation"