CS253

CS253: Software Development with C++

Spring 2017

Ios State Flags

See this page as a slide show

Ios State Flags

Overview

ios State Flags

An ios (Input/Output Sysstem) object maintains state.

Several sorts of things can go wrong, so three different types of errors are maintained:

Yes, “fail” and “bad” are quite similar words. Life is difficult.

ios::eofbit

ios::eofbit is set when end-of-file is encountered.

That’s when you try to read, but can’t, because there’s no more data. It doesn’t predict.

ifstream in("/dev/null");
if (in.rdstate() & ios::eofbit)  cout << "Before, eofbit\n";
if (in.rdstate() & ios::failbit) cout << "Before, failbit\n";
if (in.rdstate() & ios::badbit)  cout << "Before, badbit\n";
char c;
in.get(c);
if (in.rdstate() & ios::eofbit)  cout << "After, eofbit\n";
if (in.rdstate() & ios::failbit) cout << "After, failbit\n";
if (in.rdstate() & ios::badbit)  cout << "After, badbit\n";
After, eofbit
After, failbit

ios::failbit

ios::failbit is set when an I/O operation fails—you didn’t get what you asked for.

ios::failbit was set in the previous example, because we tried to read a character, and couldn’t.

Let’s try reading a double from /etc/resolv.conf, which does not contain numbers:

ifstream in("/etc/resolv.conf");
if (in.rdstate() & ios::eofbit)  cout << "Before, eofbit\n";
if (in.rdstate() & ios::failbit) cout << "Before, failbit\n";
if (in.rdstate() & ios::badbit)  cout << "Before, badbit\n";
double d;
in >> d;
if (in.rdstate() & ios::eofbit)  cout << "After, eofbit\n";
if (in.rdstate() & ios::failbit) cout << "After, failbit\n";
if (in.rdstate() & ios::badbit)  cout << "After, badbit\n";
After, failbit

ios::failbit upon open failure

Also, ios::failbit is set when a file can’t be opened.

ifstream in("/this/file/doesn’t/exist");
if (in.rdstate() & ios::eofbit)  cout << "eofbit\n";
if (in.rdstate() & ios::failbit) cout << "failbit\n";
if (in.rdstate() & ios::badbit)  cout << "badbit\n";
failbit

ios::badbit

ios::badbit is not set upon a mere conversion error, but, rather, when a low-level I/O error occurs.

The system runs out of memory, the disk becomes full, etc.

Let’s try writing to the special file /dev/full, which simulates a disk that is always full.

ofstream out("/dev/full");
if (out.rdstate() & ios::eofbit)  cout << "Before, eofbit\n";
if (out.rdstate() & ios::failbit) cout << "Before, failbit\n";
if (out.rdstate() & ios::badbit)  cout << "Before, badbit\n";
out << "foo" << endl;
if (out.rdstate() & ios::eofbit)  cout << "After, eofbit\n";
if (out.rdstate() & ios::failbit) cout << "After, failbit\n";
if (out.rdstate() & ios::badbit)  cout << "After, badbit\n";
After, badbit

The bits are sticky

ifstream in("/etc/resolv.conf");
double d;
in >> d;
if (in.rdstate() & ios::failbit) cout << "#1: failbit\n";
char c;
in >> c;
if (in.rdstate() & ios::failbit) cout << "#2: failbit\n";
#1: failbit
#2: failbit

Clearing bits

ifstream in("/etc/resolv.conf");
double d;
in >> d;
if (in.rdstate() & ios::failbit) cout << "#1: failbit\n";
char c;
in >> c;
if (in.rdstate() & ios::failbit) cout << "#2: failbit\n";
in.clear();
in >> c;
if (in.rdstate() & ios::failbit) cout << "#3: failbit\n";
cout << "c=‘" << c << "’\n";
#1: failbit
#2: failbit
c=‘s’

«ios::clear(cpp)» clears ios::badbit, ios::eofbit, and ios::failbit.

Interrogating bits

The previous slides have been interrogating the bits in a straightforward, but clumsy fashion. There are several other ways:

HowResults
«ios::rdstate(cpp)»ios::eofbit|ios::badbit|ios::failbit
«ios::good(cpp)»no bits are set
«ios::eof(cpp)»ios::eofbit is set
«ios::bad(cpp)»ios::badbit is set
«ios::fail(cpp)»ios::badbit or ios::failbit is set
! stream.fail()
stream in boolean context! .fail()

Advice

I generally only use boolean context, and occasionally .eof when I want to be rigorous. I also avoid .is_open():

ifstream in("/etc/resolv.conf");
if (!in) {
    cerr << "Can’t open /etc/resolv.conf for reading\n";
    return 1;
}

float f;

while (in >> f)
    cout << "Hooray, got a number: " << f << '\n';

if (!in.eof())
    cerr << "Error reading floats.\n";
Error reading floats.

Modified: 2017-03-19T13:51

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building