The ferror() function defined in the stdio.h header file. It helps to test for the error while performing reading or writing with the given stream.
int ferror(FILE *stream); #where stream should be a file pointer
The ferror() function takes a single parameter.To determine the exact nature of the error we can use perror() method.
Parameter | Description | Required / Optional |
---|---|---|
stream | the pointer to a FILE object that identifies the stream. | Required |
The return value of ferror() function is non-zero if the error indicator associated with the stream is set otherwise returns zero.
Input | Return Value |
---|---|
If error | non-zero |
If no error | zero |
#include <stdio.h>
int main()
{
FILE *pnt;
char chr;
pnt = fopen("myfile.txt", "w");
chr = fgetc(pnt);
if( ferror(pnt) ) {
printf("Reading error from file : myfile.txt\n");
}
clearerr(pnt);
if( ferror(pnt) ) {
printf("Reading error from file : myfile.txt\n");
}
fclose(pnt);
return(0);
}
Output:
Reading error from file "myfile.txt"
#include <stdio.h>
int main (){
FILE *pnt;
pnt = fopen("fileName.txt","w");
while(!done)
{
putc(info, pnt);
if(ferror(pnt))
{
printf("File Error..!\n");
printf("Press any key to exit..\n");
getch();
exit(1);
}
}
}
Output:
File Error..! Press any key to exit..