Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
253 views

5.4 Error Handling in File Operations

The document discusses error handling during file operations in C++. It explains that file streams inherit a stream_state member that records the file status. Functions like eof(), fail(), bad(), and good() can be used to check the status. fail() returns non-zero if input/output fails. eof() returns non-zero when end-of-file is reached. bad() returns non-zero if an invalid operation occurs. And good() returns non-zero if no errors happen. Examples are provided to demonstrate the usage of these functions.

Uploaded by

Sujatas More
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
253 views

5.4 Error Handling in File Operations

The document discusses error handling during file operations in C++. It explains that file streams inherit a stream_state member that records the file status. Functions like eof(), fail(), bad(), and good() can be used to check the status. fail() returns non-zero if input/output fails. eof() returns non-zero when end-of-file is reached. bad() returns non-zero if an invalid operation occurs. And good() returns non-zero if no errors happen. Examples are provided to demonstrate the usage of these functions.

Uploaded by

Sujatas More
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Sanjivani Rural Education Society’s

Sanjivani College of Engineering, Kopargaon-423 603


(An Autonomous Institute, Affiliated to Savitribai Phule Pune University, Pune)
NAAC ‘A’ Grade Accredited, ISO 9001:2015 Certified

Department of Computer Engineering


(NBA Accredited)

Subject- Object Oriented Programming (CO212)


Unit 5 – Files and Streams
Topic – 5.4 Error Handling in File Operations

Prof. B. B. Kotame
Assistant Professor
E-mail : kotamebhagyashricomp@sanjivani.org.in
Contact No: 9561010331
Error Handling During File Operations

While performing file operations, situations may occur like:


• A file which we are attempting to open for reading does not exist.
• File name that we are using for a new file may already exist.
• We may attempt an invalid operation such as reading post to
the end-of-file.
• There may not be any space in the disk for storing more data.
• We may use an invalid file name.
• We may attempt to perform an operation when the file is not
opened for that particular purpose.

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 2


Error Handling During File Operations

The C++ file stream inherits a “stream_state” member from the class ios.
This member records information on the status of a file that is being currently
used.
The class ios supports several member functions that can be used to read
the status recorded in a file stream.
üeof( )
üfail( )
übad( )
ügood()etc.

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 3


int fail( ) function
The fail( ) function returns a non-zero value
when input or output operation has failed.
Example -
int main() else
{ {
fstream file; cout << "Data read from file - " <<
file.open("my_file.txt“); data;
}
string data;
return 0;
file >> data; }
if(file.fail()) Output:
{ Operation not success!!!
cout << "Operation not success!!!"; Status of failbit: 1
cout << "Status of failbit: "<< file.fail() ;
}

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 4


• An alternative to fail() function is the following code:
if (!file)
{
cout << "Operation not success!!!";
return 0;
}

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 5


int eof( ) function
• The eof( ) function returns a non-zero (true) value when end-of-file is encountered
while reading; otherwise returns zero (false).
Example -
int main()
{ Output:
fstream file; data read: This|eofbit: 0
file.open("my_file.txt", ios::in); data read: is|eofbit: 0
data read: sample|eofbit: 0
string data; data read: data|eofbit: 1
while(!file.eof())
{
file >> data;
cout <<"data read:“<<data<<“|eofbit:”
<< file.eof();
}
return 0;
}
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 6
• The bad( ) function returns a non-zero (true) value if an invalid operation is
attempted or an unrecoverable error has occurred. Returns zero if it may be
possible to recover from any other error reported and continue operations.
#include <iostream> if(!file.bad()){
#include <fstream> cout << "Operation not success!!!" <<
endl;
using namespace std; cout << "Status of the badbit: " <<
file.bad() << endl;
int main() }
{ else {
fstream file; cout << "Data read from file - " << data
file.open("my_file.txt", ios::out); << endl;
}
string data; return 0;
}
file >> data; DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 7
output
Operation not success
Status of the badbit:0

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 8


• The good( ) function returns a non-zero (true) value when no error has occurred;
otherwise returns zero (false). cout << endl << "Data read from file:"
#include <iostream> << endl;
#include <fstream> while(!file.eof()){
using namespace std; file >> data;
int main() cout << data << " ";
{ }
fstream file; cout << endl;
file.open("my_file.txt", ios::in);
cout << "goodbit: " << return 0;
file.good() << endl; }
output:
string data; Data read from file:
This is sample Data
DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 9
Thank You..

DEPARTMENT OF COMPUTER ENGINEERING , SCOE,KOPARGAON 10

You might also like