Data File - Error Handling During File I/O

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:

    I have often been reproached with the aridity of my genius; a deficiency of imagination has been imputed to me as a crime; and the Pyrrhonism of my opinions has at all times rendered me notorious. Indeed, a strong relish for physical philosophy has, I fear, tinctured my mind with a very common error of this age—I mean the habit of referring occurrences, even the least susceptible of such reference, to the principles of that science.
    Edgar Allan Poe (1809–1849)

    It is curious how instinctively one protects the image of oneself from idolatry or any other handling that could make it ridiculous, or too unlike the original to be believed any longer.
    Virginia Woolf (1882–1941)

    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)