Rogue Wave banner
Previous fileTop of DocumentContentsNext file

8.2 Checking the Stream State

Let's first examine how you can check for errors using the stream state. A stream has several member functions for this purpose, which are summarized with their effects in Table 9:

Table 9 -- Stream member functions for error checking

ios_base member functionEffect
bool good()
True if no error flag is set
bool eof()
True if eofbit is set
bool fail()
True if failbit or badbit is set
bool bad()
True if badbit is set
bool operator!()
As fail()
operator void*()
Null pointer if fail() and non-null value otherwise
iostate rdstate()
Value of stream state

It is a good idea to check the stream state in some central place, for example:

The state of cout is examined with operator!(), which will return true if the stream state indicates an error has occurred.

An ostream can also appear in a boolean position to be tested as follows:

The magic here is the operator void*() that returns a non-zero value when the stream state is non-zero.

Finally, the explicit member functions can also be used:

Note that there is a difference between good() and operator!(). The function good() takes all flags into account; operator!() and fail() ignore eofbit.


Previous fileTop of DocumentContentsNext file

©Copyright 1998, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.


OEM Release, June 1998