Error handling With Streams/File
Errors : Errors are the problem that occures in the program due to an illegal operations performed by
the user.
Error handling : Error handling is the process of handling errors and exceptions so that the normal
executions of the program/system will not effected.
It is the process to handle errors in a file. We perform error handling so, the normal flow of program can
be maintained even after getting errors.
Common problems/scenarios the lead to errors during file operations:
1. When trying to open a file for reading might not exist.
2. When trying to read from file beyond its total numbers of characters.
3. When trying to perform a read operaton on a file that has opend in write mode.
4. When trying to perform a write operaton on a file that has opend in read mode.
5. When trying to operate on a file that has not been opend.
6. Etc.
C++ language provide several built-in functions to handle errors during file
operations.
1. int bad()
2. int fail()
3. int good()
4. int eof()
5. clear()
int bad() :
checks if a serious error has occurred on the stream (like hardware failure or corrupt stream).
It returns true (non-zero) when a serious, unrecoverable error occurs on the stream.
These errors can come from:
Disk failure
Hardware issues
Trying to use the stream incorrectly (rare in normal use)
Error handling With Streams/File
Sample code:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream file;
// Try to open a file at a path that likely doesn't exist or is invalid
file.open("Z:\\java\\chitkara university\\lectures\\C++\\cpp_practice_prog\\abcde.txt");
// Check for serious error using bad()
if (file.bad()) {
cout << "A serious error occurred in the file stream!" << endl;
} else {
cout << "No serious error occurred (bad() returned false)." << endl;
}
file.close();
return 0;
}
Output :
No serious error occurred (bad() returned false).
int fail()
It returns true (non-zero) when:
A file couldn't be opened.
You try to read wrong type of data (like reading int but input is abc).
Sample Code : (file not found)
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream file;
// Try to open a file at a path that likely doesn't exist or is invalid
Error handling With Streams/File
file.open("Z:\\java\\chitkara university\\lectures\\C++\\cpp_practice_prog\\abcde.txt");
// Check for serious error using bad()
if (file.fail()) {
cout << "A serious error occurred in the file stream!" << endl;
} else {
cout << "No serious error occurred (bad() returned false)." << endl;
}
file.close();
return 0;
}
Output :
A serious error occurred in the file stream!
Sample Code : (Wrong input)
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
// Check if input failed (like user entered letters)
if (cin.fail()) {
cout << " Invalid input! You did not enter an integer." << endl;
} else {
cout << " You entered: " << number << endl;
}
return 0;
}
Error handling With Streams/File
int good() :
1-> no problem found, 0-> problem found;
1. File not found
2. Wrong input type (e.g., trying to read int but input is text)
3. Reading past the end of file
4. Serious stream error
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream file("D:\\java\\chitkara university\\lectures\\C++\\cpp_practice_prog\\abcd.txt"); // Make
sure this file exists with some text
// Check initial stream state
cout << "Initial Stream State:" << endl;
cout << "good(): " << file.good() << endl;
cout << "fail(): " << file.fail() << endl;
cout << "bad(): " << file.bad() << endl;
cout << "eof(): " << file.eof() << endl;
cout << "--------------------------" << endl;
string word;
while (file >> word) {
cout << "Read word: " << word << endl;
}
// After reading all content, check the states again
cout << "\nAfter Reading File:" << endl;
cout << "good(): " << file.good() << endl;
cout << "fail(): " << file.fail() << endl;
cout << "bad(): " << file.bad() << endl;
cout << "eof(): " << file.eof() << endl;
file.close();
return 0;
}
Output:
Initial Stream State:
good(): 1
Error handling With Streams/File
fail(): 0
bad(): 0
eof(): 0
--------------------------
Read word: Hello
Read word: all
Read word: ::
After Reading File:
good(): 0
fail(): 1
bad(): 0
eof(): 1
clear() :
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream file("D:\\java\\chitkara university\\lectures\\C++\\cpp_practice_prog\\abcd.txt"); // Make
sure this file exists
// Initial stream state
cout << "Initial Stream State:\n";
cout << "good(): " << file.good() << endl;
cout << "fail(): " << file.fail() << endl;
cout << "bad(): " << file.bad() << endl;
cout << "eof(): " << file.eof() << endl;
cout << "--------------------------" << endl;
string word;
while (file >> word) {
cout << "Read word: " << word << endl;
}
// Stream has reached end of file
cout << "\nAfter Reading File:\n";
cout << "good(): " << file.good() << endl;
cout << "fail(): " << file.fail() << endl;
cout << "bad(): " << file.bad() << endl;
cout << "eof(): " << file.eof() << endl;
Error handling With Streams/File
// Clear the stream state
file.clear(); // Reset fail and eof flags
cout << "\nAfter clear():\n";
cout << "good(): " << file.good() << endl;
cout << "fail(): " << file.fail() << endl;
cout << "bad(): " << file.bad() << endl;
cout << "eof(): " << file.eof() << endl;
file.close();
return 0;
}
Output:
Initial Stream State:
good(): 1
fail(): 0
bad(): 0
eof(): 0
--------------------------
Read word: Hello
Read word: all
Read word: ::
After Reading File:
good(): 0
fail(): 1
bad(): 0
eof(): 1
After clear():
good(): 1
fail(): 0
bad(): 0
eof(): 0
eof() :
#include <iostream>
#include <fstream>
using namespace std;
Error handling With Streams/File
int main() {
ifstream file("D:\\java\\chitkara university\\lectures\\C++\\cpp_practice_prog\\G-28.txt"); // Make
sure this file exists
string s;
while(!file.eof())
{
file >> s;
cout << "data read : "<<s<<" | eof state : "<<file.eof()<<endl;
}
file.close();
return 0;
}
Output:
data read : Hello | eof state : 0
data read : all | eof state : 0
data read : from | eof state : 0
data read : Gaurav | eof state : 0
data read : Verma | eof state : 1