The “cerr” Object:
The “cerr” object is usually used for printing error messages. This object is similar to “cout” object. The difference is that unlike “cout” object, the output sent by the “cerr” object is not buffered and is displayed immediately. Thus there is a better chance of it being displayed.
The open operation can he tested with the “if-statement”. To test the open operation on a data file “student.dat”, the statements are written as:
| ifstream xyz (“student.dat”,ios::out); if( !xyz ) { cerr<< “File opening error”<<endl; exit (1); } |
The “if-statement” tests whether the data file has been successfully attached with the object “xyz” or not. If the open operation fails, the object is not created and the condition in the “if-statement” becomes true. The statements following the if-statement are executed and the error message is displayed through the “cerr” object.

Similarly, to open a file for reading data, the open operation can also be tested. To test the open operation on the data file “student.dat” for input, the statements are written as:
| ifstream xyz (“student.dat”,ios::in); if( !xyz ) { cerr<< “File opening error”<<endl; exit (1); } |
comment closed