Fclose Function In C

It has the following syntax:

Example

FILE filepointer;

int fclose( filepointer );

Parameters that need to be supplied to the function include:

A pointer pointing to a FILE object that represents the file you intend to close.

Return values that the function can produce include:

  • In case the file closing operation is successful, fclose will return zero (0).
  • If an error happens during the file closing process, a non-zero value will be returned.
  • Example to illustrate the fclose function:

Firstly, a text file named inputfile.txt is generated to handle file operations. Subsequently, data is saved within the text file. As a result, the contents of inputfile.txt will be as follows:

Example 1:

Let's consider a program to open a file and then subsequently close the file:

Example

#include<stdio.h>

int main(){

    // Open the file in read-only mode

    FILE *filepointer = fopen("inputfile.txt", "r"); 

    if (filepointer == NULL) {

perror("Error opening file");

        return 1;

    }

    // Read and print the file contents

    int character;

    while ((character = fgetc(filepointer)) != EOF) {

        // Print each character

        putchar(character); 

    }

    // Close the file after all file operations are completed

    if (fclose(filepointer) != 0) {

perror("Error closing file");

        return 1;

    }

    return 0;

}

Output:

Output

[Program Output]

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 consider another example to demonstrate the functionality of the fclose function:

Example

#include <stdio.h>

int main() {

    // Open the file in read-only mode

    FILE *filepointer = fopen("inputfile.txt", "r"); 

    if (filepointer == NULL) {

perror("Error opening file");

        return 1;

    }

printf("file is opened\n");

    // Close the file after opening it

    if (fclose(filepointer) != 0) {

perror("Error closing file");

        return 1;

    }

printf("file is closed");

    return 0;

}

Output:

Output

[Program Output]

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 .

Input Required

This code uses input(). Please provide values below: