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 an int
, you will both reach EOF and you will fail to read the int
, so both flags will be set.
The fail()
function tests badbit || 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 if badbit
or failbit
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:
if (std::cin >> x) {
// extraction succeeded
}
else {
// extraction failed
}
The operator!()
overload is the opposite of the operator void*()
; it returns true
if the badbit
or failbit
is set and false
otherwise. The operator!()
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 overload operator bool()
as an explicit conversion operator; this operator has the same semantics as the current operator void*()
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…