C Programs using File Operations - using Error Handling - perror() function
C Program to detect and printf the error message using perror() function.
/* C Program to detect and printf the error message using perror() function*/
#include <stdio.h>
#include <conio.h>
#include <process.h>
void main()
{
FILE *fr;
char c, file[] = "lines.txt";
fr=fopen(file, "w");
clrscr();
while(!feof(fr)) /* not end of the file */
{
c=fgetc(fr);
if(ferror(fr)) /* ferror() reports error */
{
perror(file); /* perror() prints compilers messages */
exit(1);
} /* if /*
else
printf("%c", c);
getch();
} /* while
fclose(fr); /* file close function closes a file */
}
OUTPUT
lines.txt: permission denied
DUMMY_CHAPTER_NAME : Tag: : - C Program to detect and printf the error message using perror() function