To retrieve the state of the current formatting flags.
iostate rdstate() const
This member function allows you to read and check the current status of the input and output formatting flags. The returned value may be stored for use in the function ios::setstate() to reset the flags at a later date.
Returns type iostate used in ios::setstate()
ios::setstate()
// The file ewl-test contains: // ABCDEFGHIJKLMNOPQRSTUVWXYZ #include <iostream> #include <fstream> #include <cstdlib> char * inFile = "ewl-test"; using namespace std; void status(ifstream &in); int main() { ifstream in(inFile); if(!in.is_open()) { cout << "could not open file for input"; exit(1); } int count = 0; int c; while((c = in.get()) != EOF) { // simulate a bad bit if(count++ == 12) in.setstate(ios::badbit); status(in); } status(in); in.close(); return 0; } void status(ifstream &in) { int i = in.rdstate(); switch (i) { case ios::eofbit : cout << "EOF encountered \n"; break; case ios::failbit : cout << "Non-Fatal I/O Error n"; break; case ios::goodbit : cout << "GoodBit set \n"; break; case ios::badbit : cout << "Fatal I/O Error \n"; break; } }
Result:
GoodBit set GoodBit set GoodBit set GoodBit set GoodBit set GoodBit set GoodBit set GoodBit set GoodBit set GoodBit set GoodBit set GoodBit set Fatal I/O Error