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
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:
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():
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 <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.