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:

    No consensus of men can make an error erroneous. We can only find or commit an error, not create it. When we commit an error, we say what was an error already.
    Josiah Royce (1855–1916)

    Madam, a circulating library in a town is as an evergreen tree of diabolical knowledge; it blossoms through the year. And depend on it ... that they who are so fond of handling the leaves, will long for the fruit at last.
    Richard Brinsley Sheridan (1751–1816)

    Probably nothing in the experience of the rank and file of workers causes more bitterness and envy than the realization which comes sooner or later to many of them that they are “stuck” and can go no further.
    Mary Barnett Gilson (1877–?)