Io condition status
The IO standard library manages a series of conditional State members to mark whether a given Io object is available or encounters a specific error.
The condition status of the IO standard library is as follows:
STRM: The integer name related to the iostate machine, defined by each iostream class, is used to define the condition state STRM: badbit type value, used to indicate the broken stream STRM:: failbit STRM: failbit type value, used to point out the failed Io operation STRM: eofbit type value, used to point that the stream has reached the file Terminator s. EOF () if the eofbit value of stream S is set, the function returns Trues. fail () if the failbit value of stream S is set, the function returns Trues. bad () if the badbit value of stream S is set, the function returns Trues. good () if the stream S is in the valid state, the function returns Trues. clear () resets all States in stream s to valid states. clear (FLAG) sets a specified status in stream s to valid. The flag type is STRM: iostates. setstate (F Lag) adds a specified condition to stream S. The flag type is STRM: iostates. rdstate () and returns the current condition of stream S. the return value type is STRM: iostate.
Badbit indicates system-level faults, such as unrecoverable read/write errors. If this type of error occurs, it usually indicates that the stream cannot continue to be used.
Failbit is generally an error that can be corrected. For example, if you expect to accept data of the numerical type but enter a character.
In general, if an error such as failbit needs to be corrected, it is necessary to clear invalid data in the buffer,
For example
cin.clear(istream::failbit)
After the failbit status is reset, use the cin. Ignore () function to clear the error data.
cin.ignore(std::numeric_limits<std::streamsize>::max(),‘\n‘);
Cin. Ignore extracts data from the stream, and the extracted data is ignored.
The first parameter is a number. If the number of extracted bytes reaches this number or the second parameter '\ n', the function is terminated. Therefore, to ensure that the byte in the stream can match '\ n ',
Generally, the value of the first parameter must be large enough.
Refresh the output buffer
cout << "hello" << flush; //flushes the buffer ;adds no data cout << "hello" << ends; //insert a null ,then flushes the buffer cout << "hello" << endl; // insert a newline ,then flushes the buffer
If the program crashes, the buffer will not be refreshed.
The standard library binds cout and CIN, that is, any attempt to read the input stream will cause the output stream buffer associated with it to be refreshed first.
Cin> ival;
As a result, the buffer associated with cout is refreshed.
Io knowledge note