ZetCode

C fclose function

last modified April 6, 2025

Proper file handling is crucial in C programming, and the fclose function plays a vital role in resource management. It ensures files are properly closed after operations, preventing memory leaks and data corruption. This tutorial explores fclose in depth, covering its syntax, usage, and best practices. Through practical examples, you'll learn how to implement secure file closing in various scenarios. Mastering fclose is essential for writing robust, efficient C programs.

What Is fclose?

The fclose function in C closes an open file and flushes any buffered data to disk. It takes a single parameter: a FILE pointer returned by fopen. The function returns zero on success and EOF on failure. Always close files when done to free system resources and ensure data integrity. Proper error handling around fclose helps detect and resolve file system issues.

Basic File Closing

This example demonstrates the fundamental usage of fclose with a simple file operation.

basic_close.c
#include <stdio.h>

int main() {
    FILE *fp = fopen("example.txt", "w");
    
    if (fp == NULL) {
        perror("Error opening file");
        return 1;
    }
    
    fprintf(fp, "This is a test file.\n");
    
    if (fclose(fp) != 0) {
        perror("Error closing file");
        return 1;
    }
    
    printf("File closed successfully.\n");
    return 0;
}

Here, we open a file in write mode, write a string, then close it with fclose. The function's return value is checked to ensure proper closure. If fclose fails, it returns EOF and sets the error indicator. This basic pattern should be used whenever working with files in C programs.

Closing Multiple Files

Learn how to properly handle and close multiple files in a single program.

multi_close.c
#include <stdio.h>

int main() {
    FILE *fp1, *fp2;
    int close_status = 0;
    
    fp1 = fopen("file1.txt", "w");
    fp2 = fopen("file2.txt", "w");
    
    if (fp1 == NULL || fp2 == NULL) {
        perror("Error opening files");
        return 1;
    }
    
    fprintf(fp1, "Content for file 1\n");
    fprintf(fp2, "Content for file 2\n");
    
    if (fclose(fp1) != 0) {
        perror("Error closing file1");
        close_status = 1;
    }
    
    if (fclose(fp2) != 0) {
        perror("Error closing file2");
        close_status = 1;
    }
    
    return close_status;
}

This example shows how to manage multiple file pointers and close them independently. Each fclose call is checked separately, and errors are tracked with close_status. This approach ensures all files get proper closure attempts, even if one fails. The program exits with a status reflecting any closure failures.

Error Handling with fclose

Proper error handling when closing files can prevent subtle bugs and data loss.

error_handling.c
#include <stdio.h>
#include <errno.h>

int main() {
    FILE *fp = fopen("important.dat", "wb");
    
    if (fp == NULL) {
        perror("Failed to open file");
        return 1;
    }
    
    // Simulate write operation
    fputs("Critical data", fp);
    
    if (fclose(fp) == EOF) {
        perror("Failed to close file");
        
        if (errno == ENOSPC) {
            printf("Disk full error detected\n");
        }
        
        return 1;
    }
    
    return 0;
}

This code demonstrates comprehensive error handling for fclose. We check for EOF return value and use errno to identify specific errors like disk full (ENOSPC). Such detailed error handling is crucial for critical operations where data integrity matters. Always consider the potential consequences of failed file closures in your applications.

Closing Files in Functions

Learn the proper way to handle file closure when files are opened within functions.

function_close.c
#include <stdio.h>

int process_file() {
    FILE *fp = fopen("data.csv", "r");
    if (fp == NULL) {
        perror("Failed to open file");
        return -1;
    }
    
    // Process file contents
    char buffer[256];
    while (fgets(buffer, sizeof(buffer), fp)) {
        printf("%s", buffer);
    }
    
    if (fclose(fp) != 0) {
        perror("Failed to close file");
        return -2;
    }
    
    return 0;
}

int main() {
    int result = process_file();
    if (result < 0) {
        printf("File operation failed with code %d\n", result);
        return 1;
    }
    
    return 0;
}

When files are opened within functions, they should also be closed within the same function. This example shows a clean pattern where fopen and fclose are in the same scope. The function returns different error codes for open vs. close failures. This approach prevents resource leaks and makes error handling more straightforward for the caller.

Using fclose with Binary Files

Binary files require the same careful closure as text files, with some additional considerations.

binary_close.c
#include <stdio.h>

int main() {
    FILE *fp = fopen("data.bin", "wb");
    if (fp == NULL) {
        perror("Failed to open binary file");
        return 1;
    }
    
    int data[] = {1, 2, 3, 4, 5};
    size_t written = fwrite(data, sizeof(int), 5, fp);
    
    if (written != 5) {
        perror("Incomplete write operation");
        fclose(fp);  // Still attempt to close
        return 1;
    }
    
    if (fclose(fp) != 0) {
        perror("Failed to close binary file");
        return 1;
    }
    
    printf("Binary file written and closed successfully\n");
    return 0;
}

Binary files opened with "b" mode (like "wb" here) follow the same fclose rules as text files. The example shows proper handling of both write and close operations. Note that we attempt to close the file even after a partial write. This ensures resources are released regardless of the write operation's success. Binary data often represents critical information, so proper closure is especially important.

Automatic File Closing with Exit

Understand how files are handled when a program terminates unexpectedly.

exit_close.c
#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *fp = fopen("temp.txt", "w");
    if (fp == NULL) {
        perror("Failed to open file");
        return 1;
    }
    
    fprintf(fp, "Some data");
    
    // Simulate an error condition
    printf("Simulating critical error...\n");
    exit(EXIT_FAILURE);
    
    // This line never reached
    fclose(fp);
    return 0;
}

This example demonstrates what happens when a program exits without explicitly closing files. While most systems will close files automatically on program termination, it's not guaranteed. More importantly, buffered data might not be flushed to disk. Always close files properly rather than relying on automatic cleanup. For critical data, consider using fflush before potential exit points.

Closing Standard Streams

The standard streams (stdin, stdout, stderr) can technically be closed, but should they be?

std_streams.c
#include <stdio.h>

int main() {
    printf("Before closing stdout\n");
    
    if (fclose(stdout) != 0) {
        perror("Failed to close stdout");
        return 1;
    }
    
    // This won't appear anywhere
    printf("After closing stdout\n");
    
    // But we can still write to stderr
    fprintf(stderr, "This goes to stderr\n");
    
    return 0;
}

While you can close standard streams with fclose, it's generally not recommended. This example shows that after closing stdout, print statements have no effect. The standard streams are special cases - they're typically opened automatically by the runtime system. Closing them can lead to unexpected behavior in library functions that expect them to be available.

Best Practices for Using fclose

Source

C fclose Documentation

This tutorial has explored the critical fclose function in C, demonstrating its proper use through practical examples. From basic file closure to advanced error handling, these techniques will help you write more robust file handling code. Remember that proper resource management is a hallmark of quality C programming.

Author

My name is Jan Bodnar, and I'm a dedicated programmer with a deep passion for coding. Since 2007, I've been sharing my expertise through over 1,400 articles and 8 e-books. With more than a decade of teaching experience, I strive to make programming accessible and engaging.

List C Standard Library.