Stream Error States

The state of a stream may be tested through bits in class ios_base. In a moment, we show how to test these bits, in the example of Fig. 15.22.

Figure 15.22. Testing error states.

(This item is displayed on pages 798 - 799 in the print version)

 1 // Fig. 15.22: Fig15_22.cpp
 2 // Testing error states.
 3 #include 
 4 using std::cin;
 5 using std::cout;
 6 using std::endl;
 8 int main()
 9 {
10 int integerValue;
12 // display results of cin functions
13 cout << "Before a bad input operation:"
14 << "
cin.rdstate(): " << cin.rdstate()
15 << "
 cin.eof(): " << cin.eof() 
16 << " " << 
17 << "
 cin.bad(): " << cin.bad() 
18 << "
 cin.good(): " << cin.good() 
19 << "

Expects an integer, but enter a character: ";
21 cin >> integerValue; // enter character value
22 cout << endl;
24 // display results of cin functions after bad input
25 cout << "After a bad input operation:"
26 << "
cin.rdstate(): " << cin.rdstate()
27 << "
 cin.eof(): " << cin.eof() 
28 << " " << 
29 << "
 cin.bad(): " << cin.bad() 
30 << "
 cin.good(): " << cin.good() << endl << endl;
32 cin.clear(); // clear stream
34 // display results of cin functions after clearing cin
35 cout << "After cin.clear()" << " " <<
36 << "
cin.good(): " << cin.good() << endl;
37 return 0;
38 } // end main
 Before a bad input operation:
 cin.rdstate(): 0
 cin.eof(): 0 0
 cin.bad(): 0
 cin.good(): 1

 Expects an integer, but enter a character: A

 After a bad input operation:
 cin.rdstate(): 2
 cin.eof(): 0 1
 cin.bad(): 0
 cin.good(): 0

 After cin.clear() 0
 cin.good(): 1

The eofbit is set for an input stream after end-of-file is encountered. A program can use member function eof to determine whether end-of-file has been encountered on a stream after an attempt to extract data beyond the end of the stream. The call


returns TRue if end-of-file has been encountered on cin and false otherwise.

The failbit is set for a stream when a format error occurs on the stream, such as when the program is inputting integers and a nondigit character is encountered in the input stream. When such an error occurs, the characters are not lost. The fail member function reports whether a stream operation has failed; usually, recovering from such errors is possible.

The badbit is set for a stream when an error occurs that results in the loss of data. The bad member function reports whether a stream operation failed. Generally, such serious failures are nonrecoverable.

The goodbit is set for a stream if none of the bits eofbit, failbit or badbit is set for the stream.

The good member function returns true if the bad, fail and eof functions would all return false. I/O operations should be performed only on "good" streams.

The rdstate member function returns the error state of the stream. A call to cout.rdstate, for example, would return the state of the stream, which then could be tested by a switch statement that examines eofbit, badbit, failbit and goodbit. The preferred means of testing the state of a stream is to use member functions eof, bad, fail and goodusing these functions does not require the programmer to be familiar with particular status bits.

The clear member function is used to restore a stream's state to "good," so that I/O may proceed on that stream. The default argument for clear is goodbit, so the statement


clears cin and sets goodbit for the stream. The statement

cin.clear( ios::failbit )

sets the failbit. The programmer might want to do this when performing input on cin with a user-defined type and encountering a problem. The name clear might seem inappropriate in this context, but it is correct.

The program of Fig. 15.22 demonstrates member functions rdstate, eof, fail, bad, good and clear. [Note: The actual values output may differ across different compilers.]

The operator! member function of basic_ios returns TRue if the badbit is set, the failbit is set or both are set. The operator void * member function returns false (0) if the badbit is set, the failbit is set or both are set. These functions are useful in file processing when a TRue/false condition is being tested under the control of a selection statement or repetition statement.

Introduction to Computers, the Internet and World Wide Web

Introduction to C++ Programming

Introduction to Classes and Objects

Control Statements: Part 1

Control Statements: Part 2

Functions and an Introduction to Recursion

Arrays and Vectors

Pointers and Pointer-Based Strings

Classes: A Deeper Look, Part 1

Classes: A Deeper Look, Part 2

Operator Overloading; String and Array Objects

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism


Stream Input/Output

Exception Handling

File Processing

Class string and String Stream Processing

Web Programming

Searching and Sorting

Data Structures

Bits, Characters, C-Strings and structs

Standard Template Library (STL)

Other Topics

Appendix A. Operator Precedence and Associativity Chart

Appendix B. ASCII Character Set

Appendix C. Fundamental Types

Appendix D. Number Systems

Appendix E. C Legacy Code Topics

Appendix F. Preprocessor

Appendix G. ATM Case Study Code

Appendix H. UML 2: Additional Diagram Types

Appendix I. C++ Internet and Web Resources

Appendix J. Introduction to XHTML

Appendix K. XHTML Special Characters

Appendix L. Using the Visual Studio .NET Debugger

Appendix M. Using the GNU C++ Debugger


C++ How to Program
C++ How to Program (5th Edition)
ISBN: 0131857576
EAN: 2147483647
Year: 2004
Pages: 627 © 2008-2020.
If you may any questions please contact us: