Open In App

Error Handling During File Operations in C

It is quite common that errors may occur while reading data from a file or writing data to a file in C. For example, an error may arise due to the following:

Failure to check for errors then the program may behave abnormally therefore an unchecked error may result in premature termination for the program or incorrect output.



Functions to Handle Errors During File Operations

Below are some Error handling functions during file operations in C:

1. ferror()

In C the library function ferror() is used to check for the error in the stream. Its prototype is written as:



 int ferror (FILE *stream);

The ferror() function checks for any error in the stream. It returns a value zero if no error has occurred and a non-zero value if there is an error. The error indication will last until the file is closed unless it is cleared by the clearerr() function. 

Example

Below is the program to implement the use of ferror():




// C program to illustrate the
// use of ferror()
#include <stdio.h>
#include <stdlib.h>
 
// Driver Code
int main()
{
    FILE* fp;
 
    // If a file is opened which does
    // not exist, then it will be an
    // error and corresponding errno
    // value will be set
    char feedback[100];
 
    int i;
    fp = fopen("GeeksForGeeks.TXT", "w");
 
    if (fp == NULL) {
        printf("\n The file could "
               "not be opened");
        exit(1);
    }
 
    printf("\n Provide feedback on "
           "this article: ");
    fgets(feedback, 100, stdin);
 
    for (i = 0; i < feedback[i]; i++)
        fputc(feedback[i], fp);
 
    // Error writing file
    if (ferror(fp)) {
        printf("\n Error writing in file");
        exit(1);
    }
 
    // Close the file pointer
    fclose(fp);
}

Output

Provide feedback on this article:  Test Feedback

Explanation: After executing this code “Provide feedback on this article:“ will be displayed on the screen and after giving some feedback “Process exited after some seconds with return value 0″ will be displayed on the screen.

2. clearerr()

The function clearerr() is used to clear the end-of-file and error indicators for the stream. Its prototype can be given as:

void clearerr(FILE *stream);

The clearerr() clears the error for the stream pointed by the stream. The function is used because error indicators are not automatically cleared. Once the error indicator for a specific stream is set, operations on the stream continue to return an error value until clearerr(), fseek(), fsetpos(), or rewind() is called.

Example

Below is the program to implement the use of clearerr()




// C program to illustrate the
// use of clearerr()
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
 
// Driver Code
int main()
{
    FILE* fp;
    char feedback[100];
 
    char c;
 
    fp = fopen("file.txt", "w");
 
    c = fgetc(fp);
    if (ferror(fp)) {
        printf("Error in reading from"
               " file : file.txt\n");
    }
    clearerr(fp);
 
    if (ferror(fp)) {
        printf("Error in reading from "
               "file : file.txt\n");
    }
 
    // close the file
    fclose(fp);
}

Output

Error in reading from file : file.txt

3. perror()

The function perror() stands for print error. In case of an error, the programmer can determine the type of error that has occurred using the perror() function. When perror() is called, then it displays a message describing the most recent error that occurred during a library function call or system call. Its prototype can be given as:

void perror (char*msg);

Example

Below is the program given below illustrates the use of perror(). Here, assume that the file “file.txt” does not exist.




// C program to illustrate the
// use of perror()
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
 
// Driver Code
int main()
{
    FILE* fp;
 
    // First rename if there is any file
    rename("file.txt", "newfile.txt");
 
    // Now try to open same file
    fp = fopen("file.txt", "r");
 
    if (fp == NULL) {
 
        perror("Error: ");
        return (-1);
    }
 
    // Close the file pointer
    fclose(fp);
 
    return (0);
}

Output

Error: : No such file or directory

Article Tags :