Error Handling During File I/O
Sometimes during file operations, errors may also creep in. For instance, a file being opened for reading might not exist. Or a file name used for a new file may already exist.Or an attempt could be made to read past the end-of-file, etc. To check for such errors and to ensure smooth processing, C++ file streams inherit stream-state members from the ios class that store the information on the status of a file that is being currently used. The current state of the I/O system is held in an integer, in which the following flags are encoded :
There are several error handling functions supported by class ios that help you read and process the status recorded in a file stream. Following table lists these error handling functions and their meaning :
These functions may be used in the appropriate places in a program to locate the status of a file stream and thereby take the necessary corrective measures. For example :
. . . ifstream fin ; fin.open ("Master") ; while (! fin.fail ( ) ) { . . . // process the file. } if (fin.eof ( ) ) { . . . // terminate the program. } else if (fin.bad ( ) ) { . . . // report fatal error. } else { fin.clear ( ) ; // clear error - state flags . . . } . . .Read more about this topic: Data File
Famous quotes containing the words error, handling and/or file:
“Mistakes are made on two counts: an argument is either based on error or incorrectly developed.”
—Thomas Aquinas (c. 12251274)
“That a good fit between parental handling and child temperament is vital to help children adapt to the imperatives of their society is a crucial concept that can be applied to other cultures.”
—Stella Chess (20th century)
“I have been a soreheaded occupant of a file drawer labeled Science Fiction ... and I would like out, particularly since so many serious critics regularly mistake the drawer for a urinal.”
—Kurt Vonnegut, Jr. (b. 1922)