TITLE: the good(), the bad(), and the fail() (Newsgroups: comp.lang.c++.moderated, 10 Nov 97) CLAMAGE: Steve Clamage >...snip.... >> Suppose the remaining contents of an input stream are "abcde" with no >> trailing whitespace, and you perform the operation >> MyStream >> MyString; >> The operation succeeds, meaning fail() is false, but eof has been >> reached, meaning eof() is true and good() is false. >...snip... CHRISTIANS: Al Christians >This is getting very confusing. If good() can be false when an >operation succeeds, then it looks like the example on p. 617 of >Stroustrup is not 100% reliable. But the Stroustrup example however >agrees with the C++ FAQ, and I have received an authoritative email >stating that the example is correct and that the reference to eof() on >p. 616 is wrong. Those would make me think that maybe good() and eof() >could both be true, but that good() means that no operation has failed >yet, which I suppose means that fail() is false. > >Can anybody quote chapter and verse from the standard on this? CLAMAGE: stephen.clamage@eng.Sun.COM (Steve Clamage) Yes. Section 27.4.5.3 "basic_ios iostate flags functions" of the draft standard says that function good() returns the result (rdstate()==0). Function rdstate() is defined to return the stream state bits. In other words, good() returns true if and only if none of the state bits (which includes the eof bit) is set. This behavior has always (going back to original iostreams in 1989) been the case. It should not be confusing. Here again is a summary: good() Everything is OK so far and the next operation might succeed. fail() The previous operation did not succeed and further attempts will be ignored. bad() Like fail(), but the failure is more serious, possibly involving a device failure, not just bad formatting. eof() The end of the input stream has been reached. eof tells you nothing about the success of the previous operation, because reaching eof is not necessarily an error. eof tells you only that no more input is available. If you want to know about the success of the operation just concluded, test fail(), or equivalently, test the stream directly. if( ! InStream.fail() ) ... ok if( InStream ) ... ok The stream converted to a boolean value is defined to be equivalent to (!fail()). If you want to know whether to try another input operation, you can test good(). If good() returns false, the operation will not succeed.