Closing a File in C
- The
fclosefunction is used to close an open file in C. - Closing a file is important to free system resources and ensure that all data is saved correctly.
- The
fclosefunction requires a pointer to aFILEobject as a parameter. - The function returns
0if the file closing was successful, orEOFin case of error. - It is good practice to explicitly close files that are no longer needed, even though the operating system automatically closes them at program termination.
Closing a file
In the previous lesson we saw how to open a file in C language using the fopen() function specifying both the file path and the opening mode. The fopen() function returns a pointer to an object of type FILE, which represents the open file and is used to perform read or write operations on the file that we will see later.
After completing read or write operations on a file, it is important to close it correctly to free the system resources associated with the file and ensure that all data is saved correctly. In C language, the function used to close a file is fclose().
The closing operation is important for a number of reasons:
-
If the file is open for writing, closing the file ensures that all written data is actually saved to disk.
In fact, write operations can be stored in a temporary buffer by the operating system and not be immediately written to the physical file. The call to
fclose()forces the writing of this data to disk. -
Closing a file frees the system resources associated with the file.
In fact, each open file consumes system resources. An operating system associates with each open file a certain amount of memory and other resources indexed by a so-called file descriptor. To prevent a process from saturating system resources, all operating systems impose a maximum limit on the number of files that a process can keep open simultaneously. Closing files that are no longer needed helps keep the number of open files within these limits. This is very important especially in applications that open many files or that operate on large files.
It should also be kept in mind that not closing a file is not a serious error in itself. In fact, when a program terminates, the operating system automatically closes all files opened by the program. However, it is good programming practice to explicitly close files that are no longer needed.
Always explicitly close files that are no longer needed
Even though the operating system automatically closes open files at program termination, it is good programming practice to explicitly close files that are no longer needed using the fclose() function.
The fclose function
Returning to the fclose() function, its syntax is as follows:
int fclose(FILE *stream);
It requires as a parameter a pointer to an object of type FILE, which represents the file to be closed. The function returns an integer value: 0 if the file closing was successful, or EOF (End Of File) in case of error.
EOF is a macro (a constant) defined in the header file <stdio.h>, which represents a negative integer value used to indicate the end of a file or an error in input/output operations.
To clarify the use of the fclose() function, let's consider the following program scheme that opens a file, called example.dat, in read mode, checks that it has been opened correctly, and then after performing some read operations (not shown here), closes the file before terminating:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file;
int result;
// Open the file in read mode
file = fopen("example.dat", "r");
if (file == NULL) {
printf("Error opening the file");
return EXIT_FAILURE;
}
// Read operations on the file
// ...
// Close the file
result = fclose(file);
if (result == EOF) {
printf("Error closing the file");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
The file opening operations can also be summarized with the following line of code:
file = fopen("example.dat", "r");
In fact, we can enclose everything in the condition of the if statement, as shown below:
if ((file = fopen("example.dat", "r")) == NULL) {
printf("Error opening the file");
return EXIT_FAILURE;
}
In this example, after opening the file and verifying that the opening was successful, we perform read operations (not shown here) and finally close the file using fclose(). If closing the file fails, an error message is printed.