fclose() Function

The fclose() function in C closes an open file stream, ensuring that any buffered data is flushed and that the file is properly disassociated from the stream. This is a crucial step in managing file resources and preventing data loss or corruption.


Syntax of fclose()

</>
Copy
int fclose(FILE *stream);

Parameters

ParameterDescription
streamA pointer to a FILE object that identifies the stream to be closed.

When fclose() is called, all internal buffers associated with the stream are flushed. Any unwritten output is written to the file, and any unread input is discarded. Even if the function call fails, the stream is disassociated from the file and its buffers.


Return Value

If the stream is successfully closed, fclose() returns 0. On failure, it returns EOF.


Examples for fclose()

Example 1: Basic File Closure

This example demonstrates how to open a file, close it using fclose(), and check for successful closure.

Program

</>
Copy
#include <stdio.h>

int main() {
    FILE *fp = fopen("example.txt", "w");
    if (fp == NULL) {
        printf("Error opening file.\n");
        return 1;
    }

    // File operations can be performed here

    // Close the file and check the return value
    if (fclose(fp) == 0) {
        printf("File closed successfully.\n");
    } else {
        printf("Error closing file.\n");
    }

    return 0;
}

Explanation:

  1. The program opens a file named example.txt in write mode.
  2. It checks if the file was successfully opened.
  3. After performing any file operations (omitted here for simplicity), fclose() is called to close the file.
  4. The return value of fclose() is checked to determine if the file was closed successfully.

Program Output:

File closed successfully.

Example 2: File Closure After Writing Data

This example shows how to write data to a file and then properly close the file to ensure that all buffered data is flushed.

Program

</>
Copy
#include <stdio.h>

int main() {
    FILE *fp = fopen("data.txt", "w");
    if (fp == NULL) {
        printf("Error opening file.\n");
        return 1;
    }

    // Write data to the file
    fprintf(fp, "Hello, World!");

    // Close the file and check if it closed successfully
    if (fclose(fp) == 0) {
        printf("Data written and file closed successfully.\n");
    } else {
        printf("Error closing the file.\n");
    }

    return 0;
}

Explanation:

  1. The file data.txt is opened in write mode.
  2. Data (“Hello, World!”) is written to the file using fprintf().
  3. The file is then closed using fclose(), ensuring that the data is flushed to disk.
  4. The return value is checked to confirm that the file closure was successful.

Program Output:

Data written and file closed successfully.

Example 3: Error Handling in File Closure

This example illustrates how to handle errors that might occur during the file closure process.

Program

</>
Copy
#include <stdio.h>

int main() {
    // Intentionally set file pointer to NULL to simulate error scenario
    FILE *fp = NULL;

    // Attempt to close an invalid file stream
    if (fclose(fp) == EOF) {
        printf("Error: Unable to close the file stream.\n");
    } else {
        printf("File stream closed successfully (unexpected).\n");
    }

    return 0;
}

Explanation:

  1. An invalid file pointer (NULL) is used to simulate an error condition.
  2. The program attempts to close the file stream with fclose().
  3. Since the file pointer is invalid, fclose() returns EOF, triggering the error message.

Program Output:

Error: Unable to close the file stream.