How to use ferror() function to check error indicators on file pointer in C

1 Answer

0 votes
#include <stdio.h>
   
int main(void)
{
    FILE *fp;
    fp = fopen("d:\\data.txt", "r");
    if (fp == NULL) perror ("Error open file");
    else
    {
        fputc('a', fp);
        if (ferror(fp)) 
        {
            printf("Error writing to file\n"); // open file with "r"
            clearerr(fp);
        }
        fclose(fp);
    }
    return 0;
}
  
  
/*
run:
  
Error writing to file
 
*/

 



answered May 3, 2016 by avibootz
...