Qingquan-Li / blog

My Blog
https://Qingquan-Li.github.io/blog/
132 stars 16 forks source link

C++ File Stream Error Testing #227

Open Qingquan-Li opened 1 year ago

Qingquan-Li commented 1 year ago

Reference: Book: Starting Out with C++ from Control Structures to Objects, by Tony Gaddis, eighth edition


Concept: All stream objects have error state bits that indicate the condition of the stream.

All stream objects contain a set of bits that act as flags. These flags indicate the current state of the steam:

Bit Description
ios::eofbit Set when the end of an input stream is encountered.
ios::failbit Set when an attempted operation has failed.
ios::hardfail Set when an unrecoverable error has occurred.
ios::badbit Set when an invalid operation has been attempted.
ios::goodbit Set when all the flags above are not set. Indicates the stream is in good condition.

These bits can be tested by the member functions:

Function Description
eof() Returns true (nonzero) if the eofbit flag is set, otherwise returns false.
fail() Returns true (nonzero) if the failbit or hardfail flags are set, otherwise returns false.
bad() Returns true (nonzero) if the badbit flag is set, otherwise returns false.
good() Returns true (nonzero) if the goodbit flag is set, otherwise returns false.
clear() When called with no arguments, clears all the flags listed above. Can also be called with a specific flag as an argument.


Example:

This example uses the showState function to display testFile's status after various operations. First, the file is created and the integer value 10 is stored in it. The file is then closed and reopened for input. The integer is read from the file, and then a second read operation is performed. Because there is only one item in the file, the second read operation will result in an error.

// This program demonstrates the return value of the stream
// object error testing member functions.
#include<iostream>
#include<fstream>
using namespace std;

// Function prototype
void showState(fstream &);

int main()
{
    int num = 10;

    // Open the file for output.
    fstream testFile("stuff.dat", ios::out);
    if (testFile.fail())
    {
        cout << "ERROR: cannot open the file.\n";
        return 0;
    }

    // Write a value to the file.
    cout << "Writing the value " << num << " to the file.\n";
    testFile << num;

    // Show the bit states.
    showState(testFile);

    // Close the file.
    testFile.close();

    // Reopen the file for input.
    testFile.open("stuff.dat", ios::in);
    if (testFile.fail())
    {
        cout << "ERROR: cannot open the file.\n";
        return 0;
    }

    // Read the only value from the file.
    cout << "Reading from the file.\n";
    testFile >> num;
    cout << "The value " << num << " was read.\n";

    // Show the bit states.
    showState(testFile);

    // No more data in the file, but force an invalid read operation.
    cout << "Forcing a bad read operation.\n";
    testFile >> num;

    // Show the bit states.
    showState(testFile);

    // Close the file.
    testFile.close();
    return 0;
}

//*****************************************************************
// Definition of function showState. This function uses           *
// an fstream reference as its parameter. The return values of    *
// the eof(), fail(), bad(), and good() member functions are      *
// displayed. The clear() function is called before the function  *
// reutrns.                                                       *
//*****************************************************************

void showState(fstream &file)
{
    cout << "File Status:\n";
    cout << " eof bit: " << file.eof() << endl;
    cout << " fail bit: " << file.fail() << endl;
    cout << " bad bit: " << file.bad() << endl;
    cout << " good bit: " << file.good() << endl;
    file.clear();   // Clear any bad bits
}
$ g++ main.cpp -o main
$ ./main
Writing the value 10 to the file.
File Status:
 eof bit: 0
 fail bit: 0
 bad bit: 0
 good bit: 1
Reading from the file.
The value 10 was read.
File Status:
 eof bit: 1
 fail bit: 0
 bad bit: 0
 good bit: 0
Forcing a bad read operation.
File Status:
 eof bit: 1
 fail bit: 1
 bad bit: 0
 good bit: 0