C++ File Error Handling
In this page:
Stream State Flags
Every stream maintains internal state flags that record what happened during the last operation: eof() reports whether the stream has reached the end of the file, while fail() reports a more general failure, such as expecting a number but finding text.
Example: Stream State Flags
#include <iostream>
#include <fstream>
int main() {
std::ifstream file("nonexistent.txt");
std::cout << "eof=" << file.eof() << " fail=" << file.fail() << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Clearing Stream Flags
Once any error flag is set, a stream refuses to perform further operations, even if the underlying problem is temporary or already resolved -- calling clear() resets those flags back to a working state so you can continue using the stream (for example, after handling a bad line of input).
Example: Clearing Stream Flags
#include <iostream>
#include <fstream>
int main() {
std::ifstream file("nonexistent.txt");
if (file.fail()) {
std::cout << "Error detected" << std::endl;
file.clear();
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Handling Missing Files
Attempting to open a file that doesn't exist doesn't throw an exception by default -- it just leaves the stream in a failed state. Checking is_open() or the stream's boolean state immediately after opening catches this before you try to read uninitialized or garbage data from a stream that was never actually connected to a file.
Example: Handling Missing Files
#include <iostream>
#include <fstream>
int main() {
std::ifstream file("nonexistent.txt");
if (!file.is_open()) {
std::cout << "File does not exist" << std::endl;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Writing Errors
A write can fail for reasons that have nothing to do with your code, such as a full disk or a read-only filesystem. Checking good() after a write confirms none of the error flags (eof, fail, or bad) are set, giving you confidence the data was actually written rather than silently dropped.
Example: Writing Errors
#include <iostream>
#include <fstream>
int main() {
std::ofstream file("data.txt");
file << "Some content";
if (file.good()) {
std::cout << "Write confirmed successful" << std::endl;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Exception Handling with Files
Calling stream.exceptions() with the appropriate flags configures a stream to throw a std::ios_base::failure exception instead of just silently setting a flag when an error occurs -- this lets you handle file errors with the same try/catch structure you'd use for other exceptional conditions in your program.
Example: Exception Handling with Files
#include <iostream>
#include <fstream>
int main() {
std::ifstream file;
file.exceptions(std::ios::failbit);
try {
file.open("nonexistent.txt");
} catch (std::ios_base::failure &e) {
std::cout << "Exception thrown for file error" << std::endl;
}
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: