| I/O Operations |
| |
| I/O Status Handling |
| |
| Associated with each stream is a set of flags indicating the I/O state of the stream. |
| |
| For example, the flag ios::eofbit indicates that no more characters can be extracted from a stream, and the flag ios::failbit indicates that some previous request failed. |
| |
| The I/O state flags can be tested by member functions; for example, cin.eof() tests whether more characters can be extracted from the standard input stream. |
| |
| The I/O state flags can be individually manipulated by using the clear() member function; for example, cout.clear(0) clears all the I/O state flags for the stream cout. |
| |
| For convenience, the '!' operator and the conversion to void* operator allow concise testing of a stream for any error. |
| |
| These operators allow you to use statements such as the following, which writes the results of the function nextline to the standard output stream until an error occurs: |
| |
| while(cout) |
| cout << nextline(); |
| |
| Because an insertion or extraction always produces its stream argument as its result, this can be further abbreviated to the following statement: |
| |
| while(cout << nextline()); |
| |
| This form makes it more obvious that the loop might not terminate at all. Note that attempting to extract from a stream from which no more characters can be taken is considered a failure. |
| |
| Thus, you can use a loop such as the following to extract and process items from a stream until the stream is exhausted: |
| |
| while(cin >> datum) |
| process(datum); |
| |
| |
|
| |
| |