w3resource

C ferror() function

C library function - ferror()

The ferror() function check for an error in reading from or writing to the given stream.When an error occurs, the error indicator remains set until you close stream, call the clearerr() function.

Syntax:

int ferror(FILE *stream)

ferror() Parameters:

Name Description Required /Optional
stream Identifies an address for a file descriptor, which is an area of memory associated with an input or output stream. Required

Return value from ferror()

  • The ferror() function shall return non-zero if and only if the error indicator is set for stream.

Example: ferror() function

Following example puts data out to a stream, and then checks that a write error (invalid filename) has not occurred.

#include <stdio.h>
 
int main(void)
{
   FILE *stream;
   char *string = "C programming.";
   stream = fopen("*test.txt","w");
 
   fprintf(stream, "%s\n", string);
   if (ferror(stream))
   {
      printf("write error\n");
      clearerr(stream);
   }
   if (fclose(stream))
      perror("fclose error");
}

Output:

fclose error: Invalid argument

C Programming Code Editor:

Previous C Programming: C feof()
Next C Programming: C fflush()



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming/stdio/c_library_method_ferror.php