Use cin.fail Method in C++

  1. Use the fail Method to Check if Error Occurs on Stream Object in C++
  2. Use good Method to Check if Error Occurs on Stream Object in C++
  3. Use Stream Object as Expression to Check if Error Occurrs in C++

This article will demonstrate multiple methods of using the fail method correctly on stream objects in C++.

Use the fail Method to Check if Error Occurs on Stream Object in C++

The fail method is the built-in function of the basic_ios class, and it can be called to verify if the given stream has an erroneous state. The function takes no arguments and returns the boolean value true if any error has occurred on the stream object. Note that stream objects have several constant bits to describe their current state. These constants are - goodbit, eofbit, failbit and badbit. If failbit is set, it means that the operation failed, but the stream itself is in a good state. badbit is set when the stream gets corrupted. fail method returns true if badbit or failbit is set on a given stream object.

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::ifstream; using std::ostringstream;

int main() {
    string filename("tmp.txt");
    string file_contents;

    auto ss = ostringstream{};
    ifstream input_file(filename);
    if (input_file.fail()) {
        cerr << "Could not open the file - '"
             << filename << "'" << endl;
        exit(EXIT_FAILURE);
    }

    ss << input_file.rdbuf();
    file_contents = ss.str();

    cout << file_contents;
    exit(EXIT_SUCCESS);
}

Use good Method to Check if Error Occurs on Stream Object in C++

Alternatively, one can call the good built-in function to check if the stream object is in a good state. Since the goodbit constant being set in the stream state means that every other bit is cleared, checking it implies the stream’s good state. Note that these constants are defined within class ios_base and may be referred to as std::ios_base::goodbit. The following code example shows that logical negation of the good method return value can be specified as a condition to check for any failures on the stream.

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::ifstream; using std::ostringstream;

int main() {
    string filename("tmp.txt");
    string file_contents;

    auto ss = ostringstream{};
    ifstream input_file(filename);
    if (!input_file.good()) {
        cerr << "Could not open the file - '"
             << filename << "'" << endl;
        exit(EXIT_FAILURE);
    }

    ss << input_file.rdbuf();
    file_contents = ss.str();

    cout << file_contents;
    exit(EXIT_SUCCESS);
}

Use Stream Object as Expression to Check if Error Occurrs in C++

The previous methods - fail and good are recommended way of evaluating stream errors in modern C++ style, as it makes code more readable. One can also specify the stream object itself as an expression to check if any error occurs on it. The latter method is more cryptic but the correct way to implement the error checking condition statements.

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>

using std::cout; using std::cerr;
using std::endl; using std::string;
using std::ifstream; using std::ostringstream;

int main() {
    string filename("tmp.txt");
    string file_contents;

    auto ss = ostringstream{};
    ifstream input_file(filename);
    if (!input_file) {
        cerr << "Could not open the file - '"
             << filename << "'" << endl;
        exit(EXIT_FAILURE);
    }

    ss << input_file.rdbuf();
    file_contents = ss.str();

    cout << file_contents;
    exit(EXIT_SUCCESS);
}

Related Article - C++ IO

  • Use the ignore() Function in C++