Loading....
Coupon Accepted Successfully!

 

Checking For Errors

Until now, we have carried out the file operations without thinking whether they are performed successfully or not. If any fault occurs during a file operation, the fault cannot be detected. Various errors can be made by the user while performing a file operation. Such errors should be reported in the program to avoid further program failure. When a user attempts to read a file that does not exist or opens a read-only file for writing purpose, the operation fails in such situations. Such errors should be reported, and proper actions have to be taken before further operations are performed.
 
The ! (logical negation operator) overloaded operator is useful for detecting errors. It is a unary operator and, in short, it is called a not operator. The (!) not operator can be used with objects of stream classes. This operator returns a non-zero value if a stream error occurs during an operation. Consider the following program:
 

16.5 Write a program to check whether the file is successfully opened or not.

#include<fstream.h>

#include<constream.h>

void main()

{

clrscr();

ifstream in (“text”);

if (!in) cerr <<“\n File is not opened”<<in;

else cerr <<“\n file is opened”<<in;

}

OUTPUT
File is opened 0x8f15ffd2
 
Explanation: In the above program, an existing file text is opened for reading. The file is opened successfully. Hence, no error is generated. The if() statement checks the contents in objects using the following statement: if (!in). The value of the object is 0x8f15ffd2. In case the operation fails, the value of the object in would be 0x8f160000.
 

16.6 Write a program to detect error in file operation using ! Operator

#include<fstream.h>

#include<constream.h>

#include<iomanip.h>

main()

{

clrscr();

char c, f_name[10];

cout<<“\n Enter file name:”;

cin>>f_name;

ifstream in(f_name);

if (!in)

{

cerr<<“ Error in opening file”<<f_name<<endl;

return 1;

}

in>>resetiosflags(ios::skipws);

while (in)
{
in>>c;
cout<<c;
}
return 0;

}

OUTPUT
Enter file name : TEXT
One Two Three Four
1 2 3 4
** The End **

 

 
Explanation: In the above program-using constructor of the class ifstream, a file is opened. The file that is to be opened is entered by the user during program execution. If the file does not exist, the ifstream object (in) contains 0. The if() statement checks the value of an object in, and if the operation fails, a message will be displayed and the program is terminated. In case the file exists, using the while() loop, the contents of the file are read one character at a time and displayed on the screen. You can observe the use of the NOT operator with object in if() and the while() statement. If the statement in>>resetiosflags (ios::skipws); is removed, the contents of the file would be displayed without space in one line. The operator >> ignores the white space character. Consider the following statement:
 

With NOT operator

if (!in)

{

statement1;

else

statement2

}

 
The NOT operator is used in association with the object. It is also possible to perform the operation without the use of the NOT operator. The following statement works opposite as compared with the above statement:
 

Without NOT operator

if (in)

{

statement1;

else

statement2

}





Test Your Skills Now!
Take a Quiz now
Reviewer Name