The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
In C programming, the error may occurred while writing into a file or reading from a file.
The ferror() function is used detect error while reading or writing a file.
Lets try to code using ferror() and have some fun.
#include <stdio.h> int main() { char ch[100]; FILE *fptr; //File pointer declaration fptr = fopen("info.txt", "w "); //the function fopen opens the text file fputs("This function is used to change the position of file pointer", fptr); fseek(fptr, 5, 0); fgets(ch, 50, fptr); if(ferror(fptr)) printf("\nError:Reading a file is not allowed in write mode. "); fclose(fptr); //the function closes the opened file return 0; }
The ferror function used to find out the errors during writing and reading a file.
The perror() function is used to display errors which are specified by compilers.
Lets try to code using perror() and have some fun.
#include <stdio.h> int main() { char ch[100]; FILE *fptr; //File pointer declaration fptr = fopen("info1.txt", "r "); //the function fopen opens the text file if(fptr == NULL) { perror("folk text file "); } fclose(fptr); //the function closes the opened file return 0; }
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019