How to Use cin.fail Method in C++
-
Use the
fail
Method to Check if Error Occurs on Stream Object in C++ -
Use
good
Method to Check if Error Occurs on Stream Object in C++ - Use Stream Object as Expression to Check if Error Occurs 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 <fstream>
#include <iostream>
#include <sstream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::ostringstream;
using std::string;
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 <fstream>
#include <iostream>
#include <sstream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::ostringstream;
using std::string;
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 Occurs 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 <fstream>
#include <iostream>
#include <sstream>
#include <vector>
using std::cerr;
using std::cout;
using std::endl;
using std::ifstream;
using std::ostringstream;
using std::string;
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);
}
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook