fclose() function in C

fclose() is the function that is used to close the file which is already opened. Closing a file is important to release the resources associated with it and to ensure that any pending data is properly flushed to the file. The fclose() function is part of the standard C library stdio.h.

Syntax of the fclose() function:

It has the following syntax:

Arguments which should be passed to the function are:

A pointer to a FILE object representing the file you want to close.

Return values which are returned by the function are:

  • If the file is successfully closed, fclose() returns zero (0).
  • If an error occurs while closing the file, it returns a non-zero value.

Example to illustrate the fclose() function:

First, we create a txt file with the name inputfile.txt to open and close a file. After that, we store some information in the text file. Now the inputfile.txt will look like this:

fclose() function in C

Example 1:

Let's take a program to open the file and close the file:

Output:

fclose() function in C

Explanation:

  • The code starts by including the standard input-output library header h. This header file contains functions and declarations needed for input and output operations.
  • The program defines the main() function, which is the entry point of the program.
  • A variable named filepointer of type FILE* is declared. This variable will be used to handle the file and keep track of its properties.
  • The fopen() function is used to open a file named "inputfile.txt" in read-only mode ("r"). It returns a pointer to a FILE structure that represents the opened file. If the file cannot be opened for any reason (e.g., it doesn't exist or there are permission issues), fopen() will return NULL. In this case, an error message is printed using perror(), and the program exits with a return code of 1, indicating an error.
  • The code enters a while loop that continues until the end of the file (EOF) is reached. Inside the loop, it reads each character from the file using the fgetc() function and stores it in the character variable. After that, it prints the character to the standard output using putchar(). This process continues until the end of the file is reached.
  • After all file operations are completed (the loop has read the entire file), the code uses fclose() function to close the file. This step is crucial to release any resources associated with the file and ensure proper handling of the file.
  • Just like when opening the file, the code checks the return value of fclose(). If fclose() returns a non-zero value, it indicates an error in closing the file. In such a case, an error message is printed using perror(), and the program exits with a return code of 1.

Example 2:

Let's another example to illustrate the fclose() function:

Output:

fclose() function in C

Explanation:

  • It opens the file "inputfile.txt" in read-only mode using the fopen() function.
  • It checks if the file was successfully opened. If fopen() returns NULL, it indicates an error, and an error message is printed using the perror() After that, the program exits with a return code of 1 to signify an error.
  • After successfully opening the file (assuming no error occurred during file opening), it immediately closes the file using the fclose()
  • It checks the return value of fclose() to ensure that the file was closed properly. If fclose() returns a non-zero value, indicating an error in closing the file, an error message is printed, and the program exits with a return code of 1.