The C++ std::ios::rdstate() function is used to manage the state of input/output streams. It returns the state of the stream as a bitmask, indicating various errors and state flags. These flags indicates the various errors, such as failbit(logical operation failed), badbit(error occured), eofbit(end of file reached).
SyntaxFollowing is the syntax for std::ios::rdstate() function.
iostate rdstate() const;Parameters
It does not accepts any parameter.
Return ValueAn object of type ios_base::iostate that can contain any combination of the following state flag member constants −
iostate value
(member constant)
indicates functions to check state flags good() eof() fail() bad() rdstate() goodbit No errors (zero value iostate)true
false
false
false
goodbit eofbit End-of-File reached on input operation false
true
false
false
eofbit failbit Logical error on i/o operation false
false
true
false
failbit badbit Read/writing error on i/o operation false
false
true
true
badbit Exceptions
If an exception is thrown, there are no changes in the stream.
Data RacesAccesses the stream object.
Concurrent access to the same stream object may cause data races.
ExampleLet's look at the following example, where we are going to check the stream state after an input operation.
#include <iostream> #include <sstream> int main() { std::stringstream a("112 TP"); int x; a >> x; if (a.rdstate() & std::ios::failbit) { std::cout << "Stream Reading Failed.\n"; } else { std::cout << "Stream Read Successfully.\n"; } return 0; }Output
Output of the above code is as follows −
Stream Read Successfully.Example
Consider the following example, where we are going to check stream state after a failed input operation.
#include <iostream> #include <sstream> int main() { std::stringstream ss("TutorialsPoint"); int number; ss >> number; if (ss.rdstate() & std::ios::failbit) { std::cout << "Stream Failed To Read.\n"; } else { std::cout << "Stream Read successfully.\n"; } return 0; }Output
Following is the output of the above code −
Stream Failed To Read.Example
In the following example, we are going to check stream state after end-of-file (EOF) operation.
#include <iostream> #include <sstream> int main() { std::stringstream a("WELCOME"); std::string b; std::getline(a, b); if (a.rdstate() & std::ios::eofbit) { std::cout << "End Of File Reached.\n"; } else { std::cout << "File Not At End.\n"; } return 0; }Output
If we run the above code it will generate the following output −
End Of File Reached.
ios.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4