I find myself repeatedly baffled by the rdstate()
flags - good()
, bad()
, eof()
, fail()
- and how they are expressed in basic_ios::operator!
, operator bool
and operator void*
.
Could somebody put me out of my misery and explain this so I never have to think twice again?
In addition to James' answer, it's important to remember that these flags indicate results of operations, so won't be set unless you perform one.
A common error is to do this:
The problem here is that
eof()
won't be set until after we try to get the last line, at which point the stream will say "nope, no more!" and set it. This means the "correct" way is:This places the check in the correct location. This is very unruly though; luckily for us, the return value for
std::getline
is the stream, and the stream has a conversion operator that allows it to be tested in a boolean context, with the value offail()
, which includeseof()
. So we can just write:There are three flags that indicate error state:
badbit
means something has gone very wrong with the stream. It might be a buffer error or an error in whatever is feeding data to the stream. If this flag is set, it's likely that you aren't going to be using the stream anymore.failbit
means that an extraction or a read from the stream failed (or a write or insertion for output streams) and you need to be aware of that failure.eofbit
means the input stream has reached its end and there is nothing left to read. Note that this is set only after you attempt to read from an input stream that has reached its end (that is, it is set when an error occurs because you try to read data that isn't there).The
failbit
may also be set by many operations that reach EOF. For example, if there is only whitespace left remaining in the stream and you try to read anint
, you will both reach EOF and you will fail to read theint
, so both flags will be set.The
fail()
function testsbadbit || failbit
.The
good()
function tests!(badbit || failbit || eofbit)
. That is, a stream is good when none of the bits are set.You can reset the flags by using the
ios::clear()
member function; this allows you to set any of the error flags; by default (with no argument), it clears all three flags.Streams do not overload
operator bool()
;operator void*()
is used to implement a somewhat broken version of the safe bool idiom. This operator overload returns null ifbadbit
orfailbit
is set, and non-null otherwise. You can use this to support the idiom of testing the success of an extraction as the condition of a loop or other control flow statement:The
operator!()
overload is the opposite of theoperator void*()
; it returnstrue
if thebadbit
orfailbit
is set andfalse
otherwise. Theoperator!()
overload is not really needed anymore; it dates back to before operator overloads were supported completely and consistently (see sbi's question "Why does std::basic_ios overload the unary logical negation operator?").C++0x fixes the problem that causes us to have to use the safe bool idiom, so in C++0x the
basic_ios
base class template does overloadoperator bool()
as an explicit conversion operator; this operator has the same semantics as the currentoperator void*()
.