C++ File Handling Error Handling During file Operation

C++ File Handling Error Handling During file Operation

In the previous article we have discuss about seekg(),seekp(),tellg(),tellp() Function. In this article we will learn about Error Handling in File Operation.

                       Introduction to Error Handling in File Operation

Sometimes during file operations, errors may Occur.

 Let us take ann example, 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. Or such as invalid operation may be performed. 

To find the solution  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 :

eofbit1 when end-of-file is encountered, 0 otherwise.
failbit1 when a non-fatal I/O error has occurred, 0 otherwise
badbit1 when a fatal I/O error has occurred, 0 otherwise
goodbit0 value

                  C++ Error Handling Function

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 :

int bad()Returns a non-zero value if an invalid operation is attempted or any unrecoverable error has occurred. However, if it is zero (false value), it may be possible to recover from any other error reported and continue operations.
int eof()Returns non-zero (true value) if end-of-file is encountered while reading; otherwise returns zero (false value).
int fail()Returns non-zero (true) when an input or output operation has failed.
int good()Returns non-zero (true) if no error has occurred. This means, all the above functions are false. For example, if fin.good() is true, everything is okay with the stream named as fin and we can proceed to perform I/O operations. When it returns zero, no further operations can be carried out.
clear()Resets the error state so that further operations can be attempted.

The above functions can be summarized as eof() returns true if eofbit is set; bad() returns true if badbit is set. The fail() function returns true if failbit is set; the good() returns true there are no errors. Otherwise, they return false.


Name Views Likes
C++ Exception Handling-|| 67 3
C++ Exception Handling 102 3
C++ File Handling EOF 81 5
C++ File Handling Error Handling During file Operation 930 5
C++ File Handling seekp() Function 90 5
C++ File Handling seekg() Function 78 6
C++ File Handling tellg() Function 107 5
C++ File Handling tellp() Function 95 5
C++ File Handling close() Function 69 5
C++ File Handling:: open() 89 5
C++ File Handling Introduction 121 5
C++ Container Library:: std ::array-II 92 4
Top Django Interview Questions and Answers You Need to Know 264 6
C++ Container Library::std::array 85 6
C++ Container Library Introduction 94 5
Essential Engineering Skills for Your Resume 115 5
Mistakes To Avoid During Technical Interview 122 5
Mistakes You Must Avoid During Work From Home 164 6
Seven Common Mistakes that Beginners Should Avoid While Learning to Code 94 6
Simple Linear Regression 89 6
Python wagtailmenus 108 6
Python wagtail application 109 6
Python guppy GSL 119 5
Python wagtail guppy subpackage heapy 163 6
Python guppy Introduction 204 6
Python wagtail Structural Block 242 6
Python wagtail Integrating into Django Project 335 6
Python wagtail testing 265 7
Python wagtail Basic Block 314 7
Python wagtail image tag 209 7
Python wagtail Templates 167 6
Python wagtail Backends 187 6
Python wagtail Indexing 172 6
Python wagtail Search 166 7
Python wagtail Snippets 179 6
Python wagtail Writing your own page models 156 9
Python wagtail model class PageRevision 181 10
Python wagtail model class Site 191 9
Python wagtail wagtail core model 210 9
Python wagtail page models 179 10
Python wagtail 154 11