It is quite common that an error may occur while reading data from or writing data to a file.If we fail to check for errors, then the program may behave abnormally. Therefore, an unchecked error may result in premature termination of the program or incorrect output.
ERROR
HANDLING DURING FILE OPERATIONS
It
is quite common that an error may occur while reading data from or writing data
to a file. For example, an error may arise
•
when trying to read a file beyond EOF indicator
•
when trying to read a file that does not exist
•
when trying to use a file that has not been opened
•
when trying to use a file in an inappropriate mode, i.e., writing data to a
file that has been opened for reading
•
when writing to a file that is write-protected (i.e., trying to write to a
read-only file)
If
we fail to check for errors, then the program may behave abnormally. Therefore,
an unchecked error may result in premature termination of the program or
incorrect output.
Programming Tip: An
error will be generated if you try to read a file that is opened in w mode and
vice versa.
In
C, the library function ferror() is used to check for errors in the stream. Its
prototype can be given as tream). to
int ferror (FILE *stream);
The
ferror() function checks for any errors in the stream. It returns value zero if
no errors have occurred and a non-zero value if there is an error. The error
indication will last until the file is closed unless it is cleared by the
clearerr() function. Look at the code given below which uses the ferror ().
#include <stdio.h>
main()
{
FILE *fp;
char feedback [100];
int i;
fp =
fopen("Comments.TXT", "w");
if (fp==NULL)
{
printf("\n The file could not
be opened");
exit(1);
printf("\n Provide feedback on
this book: ");
gets (feedback);
for (i 0; i < feedback [i]; i++)
fputc(feedback [i], fp);
if (ferror (fp))
{
printf("\n Error writing in
file");
exit(1);
}
fclose(fp);
}
When
you execute this code and an error occurs while writing the feedback, the
program will terminate and a message indicating 'Error writing in file' will be
displayed on the screen.
The
function clearerr () is used to clear the end-of- file and error indicators for
the stream. Its protoype can be given as
void clearerr (FILE *stream);
The
clearerr() clears the error for the stream pointed to by stream. The function
is used because error indicators are not automatically cleared; once the error
indicator for a specified stream is set, operations on that stream continues to
return an error value until clearerr, fseek, fsetpos, or rewind is called. Look
at the code given below which use the clearerr()
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
main()
{
FILE *fp;
fp=fopen("Comments.TXT",
"w");
if (fp==NULL)
{
perror("OOPS ERROR");
printf("\n error no =
%d", errno);
exit(1);
}
printf("\n Kindly give the
feedback on this book: ");
gets (feedback);
for (i = 0; i < feedback [i];
i++)
{
fputc(feedback [i], fp);
if (ferror (fp))
{
clearerr (fp);
break;
/* clears the error indicators and
jump out of for loop */
}
}
// close the file
fclose(fp);
}
The
function perror () stands for print error. In case of an error, the programmer
can determine the type of error that has occurred using the perror () function.
The perror () function defined in stdio.h header file is used to handle errors
in C programs. When called, perror () displays a message on stderr describing
the most recent error that occurred during a library function call or system
call. The prototype of perror () can be given as
void perror (char *msg);
The
perror() takes one argument msg which points to an optional user-defined
message. This message is printed first, followed by a colon, and the
implementation-defined message that describes the most recent error.
If
a call to perror() is made when no error has actually occurred, then a 'No
error' will be displayed. The most important thing to remember is that a call
to perror () does nothing to deal with the error condition. It is entirely up
to the program to take action. For example, the program may prompt the user to
do something such as terminate the program.
Usually
the program's action will be determined by checking the value of errno and the
nature of the error. In order to use the external constant errno, you must
include the header file ERRNO. H. The program given below illustrates the use
of perror (). Here we assume that the file Comments.TXT does not exist.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
main()
{
FILE *fp;
fp =
fopen("Comments.TXT", "w");
if (fp==NULL)
{
perror("OOPS ERROR");
printf("\n error no =
%d", errno);
exit(1);
}
printf("\n Provide feedback on
this book: ");
gets (feedback);
for (i=0; i<feedback [i]; i++)
fputc(feedback [i], fp);
fclose(fp);
}
Output
OOPS ERROR: No such file or directory
errno =2
Programming in C: Unit V: File processing : Tag: : Syntax with Example C Programs - Error Handling During File Operations
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation