13.8. Stream Error States

The state of a stream may be tested through bits in class ios_base. Earlier in the book, we indicated that you can test, for example, whether an input was successful. Figure 13.22 shows how to test these state bits. In industrial-strength code, you’ll want to perform similar tests on your I/O operations.


 1   // Fig. 13.22: fig13_22.cpp
 2   // Testing error states.
 3   #include <iostream>
 4   using namespace std;
 5
 6   int main()
 7   {
 8      int integerValue;
 9
10      // display results of cin functions
11      cout << "Before a bad input operation:"
12         << " cin.rdstate(): " << cin.rdstate()
13         << "     cin.eof(): " << cin.eof()    
14         << "    cin.fail(): " << cin.fail()   
15         << "     cin.bad(): " << cin.bad()    
16         << "    cin.good(): " << cin.good()   
17         << " Expects an integer, but enter a character: ";
18
19      cin >> integerValue; // enter character value
20      cout << endl;
21
22      // display results of cin functions after bad input
23      cout << "After a bad input operation:"
24         << " cin.rdstate(): " << cin.rdstate()
25         << "     cin.eof(): " << cin.eof()    
26         << "    cin.fail(): " << cin.fail()   
27         << "     cin.bad(): " << cin.bad()    
28         << "    cin.good(): " << cin.good() << endl << endl;
29
30      cin.clear(); // clear stream
31
32      // display results of cin functions after clearing cin
33      cout << "After cin.clear()" << " cin.fail(): " << cin.fail()
34         << " cin.good(): " << cin.good() << endl;
35   } // end main


Before a bad input operation:
cin.rdstate(): 0
    cin.eof(): 0
   cin.fail(): 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
   cin.fail(): 1

    cin.bad(): 0
   cin.good(): 0

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


Fig. 13.22. Testing error states.

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

cin.eof()

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 and no characters are input (e.g., when you attempt to read a number and the user enters a string). 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 stream’s error state. Calling cout.rdstate, for example, would return the stream’s state, 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 good—using these functions does not require you 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

cin.clear();

clears cin and sets goodbit for the stream. The statement

cin.clear( ios::failbit )

sets the failbit. You 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’s correct.

The program of Fig. 13.22 demonstrates member functions rdstate, eof, fail, bad, good and clear. 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.214.146