C Programming

Ferror() Function in C Language

It is common for us to read and write the files with our code as this is a powerful tool that allows us to store or access any type of data that we previously stored on the computer

Currently, there are countless types of files, from spreadsheets or databases, created by the user to system files that allow the operation of OS or other programs.

Good file management improves the integrity of their data. Therefore, it is important to open, edit and close them in the correct way, and to detect when errors occur during these operations.

In this Linux Hint article about the C language, we will explain how to use the ferror() function to detect the file management errors. We will look at the syntax of this function, the input and output arguments used, and the accepted data type. Then, we will look at the implementation of this function along with other complementary functions and show the step by step process on how to handle the files and how to use ferror() correctly using code fragments and images.

Syntax of the Ferror() Function in the C Language

int err ferror (FILE * f_Ptr);

Ferror() Function Description in the C Language

The ferror() function queries the error flag associated with the file referenced by f_Ptr in its input argument.

If the NOT error occurs, “0” is returned in err. If the error flag is active, ferror() returns a positive result that is greater than zero. If the error flag is active, it must be deactivated again with the clearerr() function. Otherwise, it remains in that state until the application that opened the file is closed.

The ferror() is part of the standard input/output library and is defined in the “stdio.h” header. To use this resource, we need to include it in our code file as follows:

#include <stdio.h>

Once the “stdio.h” header is included, we can use all the file handling features that we will see in the next section.

How to Use the Ferror() Function in the C Programming Language to Check Whether an Error Occurs While Processing a File

In this example, we will show you the step-by-step process on how to use the ferror() function to check whether an error occurs while processing a file. To do this, we create the code that uses the various functions to open, close, and read the files and use the ferror() function to check if no errors occurred.

The file that we will use in this example is what we previously created in “Documents” under the “LH example.txt” name and write a fragment of Einstein in it.

The first step is to define the f_Ptr pointer for the file that we use. This pointer is the output argument for fopen() and the input argument for ferror() and the other file processing functions we use. The following snippet shows the definition of f_Ptr and opens the file with fopen():

#include <stdlib.h>

#include <stdio.h>

void main()
   {
    FILE *f_Ptr;
    char buffer[250];
    f_Ptr = fopen( "Documents/LH example.txt" , "r" );
      fread (buffer, 120, 1, f_Ptr);
    printf ( "%s", buffer);
    if ( ferror (f_Ptr) == 0 )
         printf ( "The file was read successfully \n" );

    fclose (f_Ptr);
   }

After opening the file, it reads its contents and calls the ferror() function in the if condition to check the status of this flag. If the file is read successfully, it returns “0” as the result. So, the program falls into the if condition and displays the following message in the command console, “The file was read successfully”. Then, we close the file with the fclose() function.

Errors Returned by File Processing Functions in the C Language

In this section, we will look at how to spot the errors in the various file processing functions.

The fopen() function returns “0” if the specified file in the path of its input argument could not be opened. This can be due to various errors such as the file which is not existing in the specified path or being used by another program. The fclose() function returns “0” if the file is successfully closed. A non-zero number is returned if an error occurs.

In the following example, we use the previous snippet and use these error returns to verify that the file is successfully opened and closed:

#include <stdlib.h>

#include <stdio.h>

void main()
   {
    FILE *f_Ptr;
    char buffer[250];
      f_Ptr = fopen( "Documents/LH example.txt" ,"r" );
    if ( f_Ptr != 0);
           printf ( "The file was open successfully \n" );
      fread (buffer, 120, 1, f_Ptr);
    printf ( "%s", buffer);
    if ( ferror (f_Ptr) == 0 )
         printf ( "The file was read successfully \n" );

      if (fclose (f_Ptr) == 0);
          printf ( "The file was close successfully \n" );
   }

The following image shows the results of each of the steps which are performed to open, read, and close the file:

Conclusion

In this Linux Hint article, we showed you how to use the ferror() function to detect the file processing errors. We looked at the theoretical part of this function and applied it in a practical example to show you how to detect a read error. We also explained how you can detect the errors in the complementary functions to open and close the files using fopen() and fclose(). We hope that you found this article useful. For more articles like this, use our search engine.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).