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:

    There are strange flowers of reason to match each error of the senses.
    Louis Aragon (1897–1982)

    Many more children observe attitudes, values and ways different from or in conflict with those of their families, social networks, and institutions. Yet today’s young people are no more mature or capable of handling the increased conflicting and often stimulating information they receive than were young people of the past, who received the information and had more adult control of and advice about the information they did receive.
    James P. Comer (20th century)

    While waiting to get married, several forms of employment were acceptable. Teaching kindergarten was for those girls who stayed in school four years. The rest were secretaries, typists, file clerks, or receptionists in insurance firms or banks, preferably those owned or run by the family, but respectable enough if the boss was an upstanding Christian member of the community.
    Barbara Howar (b. 1934)