by Dinesh Thakur Category: File Handling

Function clearerr ()

The function prototype is as given below.

void(clearerr(FILE* Stream);

The function clears the end of file and error indicator for the argument stream

Function feof ()

The function prototype for this function is written in the following manner:

int feof(FILE*Stream);

The function finds if the EOF indicator is set for the stream pointed to by the argument. If so, it returns a non-zero value.

Function ferror ()

The function prototype is written as shown below.

int ferror(FILE*stream);

The function finds the status of error indicator with the stream. The function returns zero if there is no error, otherwise returns a non-zero value. The function perror () may be used to find the type of the error.

Function perror ()

The function prototype is written as given below.

void perror(const char* str);

The function displays a string giving error message as predefined in compiler for a particular error condition.

Illustrates functions feof (), clearerr (),and ferror ()

#include <stdio.h>

#include <stdlib.h> // for exit function
void main()
  FILE* Fptr;
  int i, j, k[6];
  Fptr = fopen("file1","w");
  if (Fptr ==NULL)
     printf("File could not be opened.");
     fprintf(stdout, "File is open. Enter data.\n");
     for(i =0; i <6; i++)
     putw( k [i] = 5*i, Fptr );
     Fptr = fopen ("file1","r");
     printf("The values of elements of array k are as below.\n");
     for (j =0; j<7; j++)
     printf("%d\t", getw(Fptr));
     printf ("\n");
     if (feof(Fptr))
                  printf("File end has occurred.\nfeof = %d\n", feof(Fptr));
                  printf("File end has not occurred.\nfeof = %d\n", feof(Fptr) );
                  printf("ferror = %d\n", ferror(Fptr));
                  printf("After clearerr action.\n");
                      printf("File end has occurred. \nAnd feof = %d\n", feof(Fptr));
                      printf("File end has not occurred.\nAnd feof = %d\n",feof(Fptr));
                      printf("ferror = %d\n", ferror(Fptr));

Error-Handling functions in C

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.