Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between !std::basic_ios::fail() and std::basic_ios::good()?

Tags:

c++

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?

like image 296
redoranish Avatar asked Nov 27 '25 10:11

redoranish


2 Answers

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"

like image 33
Kaleb Anderson Avatar answered Nov 29 '25 23:11

Kaleb Anderson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!