std::ios_base::iostate
From cppreference.com
typedef /*implementation defined*/ iostate; |
||
static constexpr iostate goodbit = 0; |
||
static
constexpr iostate badbit =
/* implementation defined */
static constexpr iostate failbit = /* implementation defined */ |
||
Specifies stream state flags. It is a BitmaskType
Constant | Explanation |
goodbit | no error |
badbit | irrecoverable stream error |
failbit | input/output operation failed (formatting or extraction error) |
eofbit | associated input sequence has reached end-of-file |
The eofbit
The eofbit is set by the following standard library functions:
- The string input function std::getline
- The numeric input overloads of basic_istream::operator>> if the end of the stream was encountered while reading the next character, on Stage 2 of num_get::get processing. Depending on the parsing state,
failbit
may or may not be set at the same time: for example, int n; istringstream buf( "1" ) ; buf >> n; setseofbit
, but notfailbit
: the integer 1 was successfully parsed and stored inn
. On the other hand, bool b; istringstream buf( "tr" ) ; buf >> boolalpha >> b; sets botheofbit
andfailbit
: there was not enough characters to complete the parsing of the boolean true - The character extraction overloads of operator>>std::basic_istream
- The std::get_time I/O manipulator and any of the std::time_get parsing functions: time_get::get, time_get::get_time, time_get::get_date
- The std::get_money I/O manipulator and money_get::get
- The basic_istream::sentry constructor, executed at the beginning of every formatted input function: unless the
skipws
bit is unset (e.g. by issuing std::noskipws), sentry reads and discards the leading whitespace characters. If the end of the input stream is reached during this operation, botheofbit
andfailbit
- The I/O manipulator std::ws, if it reaches the end of the stream while consuming whitespace (but, unlike the formatted input sentry, it does not set
failbit
- The unformatted input functions basic_istream::read, basic_istream::get, basic_istream::peek, basic_istream::readsome, basic_istream::ignore, and basic_istream::getline
- The discard input function basic_istream::ignore, when reaching the end of the stream before reaching the specified delimiter character.
- The immediate input function basic_istream::readsome, if basic_streambuf::in_avail returns -1
The following functions clear eofbit
as a side-effect:
Note that in nearly all situations, if eofbit is set, the failbit is set as well.
The failbit
The failbit is set by the following standard library functions:
- The basic_istream::sentry constructor, executed at the beginning of every input function, if either
eofbit
orbadbit
- The basic_ostream::sentry constructor, executed at the beginning of every output function, under implementation-defined conditions.
- operator>>(std::basic_string<>)
- operator>>(std::complex<>)
- The character array and single character overloads of operator>> if they fail to extract any characters.
- The streambuf overload of basic_istream::operator>>
- The streambuf overload of basic_ostream::operator<< if the function inserts no characters.
- operator>>(std::bitset<>)
- std::getline if the function extracts no characters or if it manages to extract basic_string::max_size
- The numeric, pointer, and boolean input overloads of basic_istream::operator>> (technically, the overloads of num_get::get
- The time input manipulator std::get_time (technically, time_get::get
- The currency input manipulator std::get_money (technically, money_get::get
- The extraction operators of all RandomNumberEngines
- The extraction operators of all RandomNumberDistributions
- The unformatted input functions basic_istream::get if they fails to extract any characters.
- basic_istream::getline
- basic_istream::read, if the end-of-file condition occurs on the input stream before all requested characters could be extracted.
- basic_istream::seekg on failure
- basic_ostream::tellp on failure
- The constructors of std::basic_fstream, std::basic_ifstream, and std::basic_ofstream
- basic_fstream::open, basic_ifstream::open, and basic_ofstream::open
- basic_fstream::close, basic_ifstream::close, and basic_ofstream::close
The badbit
The badbit is set by the following standard library functions:
- basic_ostream::put if it fails to insert a character into the output stream, for any reason.
- basic_ostream::write if it fails to insert a character into the output stream, for any reason.
- Formatted output functions operator<<, std::put_money, and std::put_time
- basic_ios::init when called to initialize a stream with a null pointer for
rdbuf()
. -
basic_istream::putback and basic_istream::unget when called on a stream with a null
rdbuf()
- basic_ostream::operator<<(basic_streambuf*) when a null pointer is passed as the argument.
- basic_istream::putback and basic_istream::unget if rdbuf()->sputbackc() or rdbuf()->sungetc() return traits::eof()
-
basic_istream::sync, basic_ostream::flush, and every output function on a
unitbuf
output stream, if rdbuf()->pubsync() returns -1 - Every stream I/O function if an exception is thrown by any member function of the associated stream buffer (e.g.
sbumpc()
,xsputn()
,sgetc()
,overflow()
, etc). - ios_base::iword and ios_base::pword on failure (e.g. failure to allocate memory).
Example
This section is incomplete Reason: no example |
See also
The following table shows the value of basic_ios accessors (good(), fail(), etc.) for all possible combinations of ios_base::iostate
ios_base::iostate flags | basic_ios accessors
| |||||||
eofbit
|
failbit
|
badbit
|
good() | fail() | bad() | eof() | operator bool | operator! |
false | false | false | true | false | false | false | true | false |
false | false | true | false | true | true | false | false | true |
false | true | false | false | true | false | false | false | true |
false | true | true | false | true | true | false | false | true |
true | false | false | false | false | false | true | true | false |
true | false | true | false | true | true | true | false | true |
true | true | false | false | true | false | true | false | true |
true | true | true | false | true | true | true | false | true |
returns state flags (public member function of std::basic_ios<CharT,Traits> ) |
|
sets state flags (public member function of std::basic_ios<CharT,Traits> ) |
|
modifies state flags (public member function of std::basic_ios<CharT,Traits> ) |