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 functions11      cout << "Before a bad input operation:" 12         << "\ncin.rdstate(): " << cin.rdstate()13         << "\n    cin.eof(): " << cin.eof()    14         << "\n   cin.fail(): "

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.