C Programming

Fclose() Function in C

Undoubtedly, reading and writing files is a powerful resource to store and dispose all kinds of data that we create. For example, we can store or create databases for further extraction and processing, program files, etc.

Nowadays, there are innumerable types of files from user-created spreadsheets and administrative files to system files that run the operating system or other programs.

Therefore, both the OS and the programs that run on it or the information that the user stores on his computer depend on the correct handling of the files to avoid the loss of information or errors in the stored data or in the programs that access them.

A common mistake in handling the files is that they are not closed after use. This means that the information can be lost or that another program that uses the file cannot access it because another program is currently using it.

In this Linux Hint article, we will show you how to use the fclose() function to correctly close the files that we previously opened for extraction or data usage.

We will look at the syntax of this function and the type of input argument it accepts. We then implement this and the other file management functions using the working examples with captcha snippets so you can see how to use the fclose() function correctly.

Syntax of the Fclose() Function in the C Language

int fclose (FILE * f_Ptr);

Fclose() Function Description in the C Language

The fclose() function closes the open file whose pointer is f_Ptr. If the file is closed correctly, it returns “0”. If there is an error in closing the file, it returns EOF.

This function is a complement to fopen(), since any file that is opened with it should be closed with fclose(). When we open a file with fopen(), it creates a buffer with its data and returns the pointer to it in f_Ptr.

After opening and until closing with fclose(), the file is in use and denies the access to other applications. When closed with fclose(), the data in the buffer is flushed to the file if the write attribute allows it and is released for use by other applications.

The fclose() is part of the standard input/output library. To use it, we need to add the “stdio.h” header to our code as follows:

#include <stdio.h>

Once the “stdio.h” header is included, we can use all the file processing functions that we will cover in the next section.

How to Properly Close a File with the Fclose() Function in the C Language

In this example, we will open a text file, extract its contents, and close it properly with fclose(). For this purpose, we create the “LH example.txt” file in “Documents” and write an Einstein fragment in it.

The first step is to define the f_Ptr pointer for the file that we want to extract. We also define the c_ data of char type which is the output of the getc() function in the retrieval loop.

To open the file, we call the fopen() function which passes the name and path as input arguments where we save the file and the read mode “r” separated by commas. In this case, the path is the “Documents/ LH example .txt” file that we created manually earlier.

As an output argument to fopen(), we send the f_Ptr pointer that we defined earlier.

Once the file is open, the program starts a loop in which it extracts all the data up to the end of the file one by one and outputs it to the screen.

#include <stdlib.h>

#include <stdio.h>

void main()
   {
    FILE *f_Ptr;
char c_;
    f_Ptr = fopen( "Documents/LH example.txt" , "r" );
    c_ = getc(f_Ptr);
while ( ! feof ( f_Ptr ) )

         {
          printf( "%c",c_ );
          c_ = getc(f_Ptr);
         }

   }

The following image shows the data which is extracted from the file and is displayed in the command console:

After extracting the data, we close the file from the if condition by calling the fclose() and passing the f_Ptr pointer as the input argument.

If the file is closed correctly, fclose() returns 0. The program abandons the condition by printing the following message to the command console – “The file close successful”:

#include <stdlib.h>

#include <stdio.h>

void main()
   {
    FILE *f_Ptr;
char c_;
    f_Ptr = fopen( "Documents/LH example.txt" , "r" );
    c_ = getc( f_Ptr );
while ( ! feof ( f_Ptr ) )

         {
          printf( "%c",c_ );
          c_ = getc( f_Ptr );
         }
if ( fclose( f_Ptr ) == 0 )
        printf( "The file close successful \n" );
   }

The following image shows the result of the full code with the correct closing of the file and the corresponding message on the screen:

Conclusion

In this Linux Hint article, we showed you the correct way to close a file using the fclose() function. We looked at the syntax and the type of input and output arguments and described how the function works. We also saw a complete example on how to define the pointers and variables that are needed to use the various file processing functions. With them, we opened and extracted the data and thenclosed the file with fclose(). We hope that you found this article useful. For more articles about the C language, use the search engine on our website.

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