4.6. Error Handling During File Operations
Q22. Explain error handling during file I/O.
Answer:
While performing input/output operations on the file one may encounter any of the following unknown conditions.
A file being opened doesn’t exist,
The file name chosen for a new file already exists.
The possibility of the disk being full.
Using a disk that is write protected.
Using an invalid name for a file.
An attempt to perform an operation when the file is not open for that purpose and so on.
Checking and handling all these unknown conditions is called error handling during file operations.
The C++ file stream uses a stream-state member of the is class. This member maintains the information on the status of a file that is currently being used. This member also uses bit fields to store the status of error conditions. The meaning of each bit of the stream-state is shown below,
eof bit
This bit indicates having reached of end-of-file.
fail bit
This bit indicates that an operation failed. It may be due to formatting,
bad bit
This bit indicates some invalid Operation occurred or something is wrong with the buffer.
hard fail
This bit indicates irrecoverable error.
The function ios ∷ tdstate() is used to obtain the value of the stream-state variable.
The ios class provides several functions like eof(), good () fail() and bad( ) to read the status recorded ina file stream.
These functions are discussed below, .
(a) eof()
This function returns true (or non-zero value) on reaching end-of-file while reading i.e., if EOF flag is set otherwise it returns false (zero).
(b) fail()
This function returns true when an input/output operation has failed, i.e. if fail bit, bad bit or hard fail flag is set.
(c) bad()
If an invalid operation is performed or any irrecoverable error has ‘occurred then this function returns true ie., if bad bit or hardfail flag is set. However it returns false if it is possible to recover from any other reported error and continue the operation.
(d) good()
If no error has occurred then this function returns true. That is, all the above functions are false and I/O flag is set. And a program can proceed to perform I/O operations. If this function returns false then it means that the program can’t perform further Operations.
(e) clear()
When this function is called with no arguments, it clears all error bits. This is also used to set specified flags. For example clear (ios :: failbit).
The following program illustrates the use of these functions. Assume that the file “Example.txt” already exists.
Program
#include<fstream.h>#include<stdlib.h>#include<conio.h>void main(){void warning(ofstreame&);ofstream str;clrscr();str.open(“Example.txt”, ios∷ noreplace);if(!str){warning(str);exit(1);}else{str<<“This text is written into the file example.txt’”;if(!str){warning(str);exit(2);}str.close();}void warning(ofstream &str){cout << endl <<“Unable to open file example.txt”;cout <<endl <<“Error state =” <<str.rdstate();cout <<endl <<“Good =” <<str.good();cout <<endI <<“EOF =” <<str.eof();cout <<endl <<“Fail =” <<str.fail();cout <<end] <<“Bad+” <<str.bad();getch();}