Rogue Wave banner
Previous fileTop of DocumentContentsIndex pageNext file
Standard C++ Library Module User's Guide
Rogue Wave web site:  Home Page  |  Main Documentation Page

29.2 Checking the Stream State

Let's first examine how you can check for errors using the stream state. Each stream class template inherits several member functions from the basic_ios template for this purpose, which are summarized with their effects in Table 30:

Table 30: Stream member functions for error checking 

ios_base member function Effect

bool good()

true if no error flag is set, false otherwise

bool eof()

true if eofbit is set, false otherwise

bool fail()

true if failbit or badbit is set, false otherwise

bool bad()

true if badbit is set, false otherwise

bool operator!()

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 returns true if the stream state indicates that an error occurred.

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

The magic here is the operator void*(), which returns a nonzero value when the stream state is nonzero.

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 DocumentContentsIndex pageNext file

Copyright (c) 1994-2006 Rogue Wave Software, a Quovadx Division.
Licensed under the Apache License, Version 2.0.
Contact Rogue Wave about documentation or support issues. You can also seek help from other developers through the Apache stdcxx community (see below).

For more information on the Rogue Wave Standard C++ Library under open source, see Section 1.2.