C fflush() Function | Flush File Buffers in C

Introduction

The fflush() function in C is a standard library function that flushes the output buffer of a stream. It is part of the C standard library (stdio.h) and is used to ensure that all data written to the output buffer is actually sent to the output device or file.

fflush() Function Syntax

The syntax for the fflush() function is as follows:

int fflush(FILE *stream);

Parameters:

  • stream: A pointer to a FILE object that specifies the output stream to be flushed. If stream is NULL, fflush() flushes all open output streams.

Returns:

  • The function returns 0 if the flush operation is successful. If an error occurs, EOF is returned.

Examples

Flushing the Standard Output

To demonstrate how to use fflush() to flush the standard output stream, we will write a simple program.

Example

#include <stdio.h>

int main() {
    // Print a message to the standard output
    printf("This is a message without a newline");

    // Flush the standard output
    fflush(stdout);

    // Wait for user input to see the effect
    getchar();

    return 0;
}

Output:

This is a message without a newline

Flushing a File Stream

This example shows how to use fflush() to flush a file stream.

Example

#include <stdio.h>

int main() {
    FILE *file;

    // Open the file for writing
    file = fopen("example.txt", "w");
    if (file == NULL) {
        printf("Error: Could not open file for writing.\n");
        return 1;
    }

    // Write some text to the file
    fprintf(file, "This is some text.");

    // Flush the file stream
    if (fflush(file) != 0) {
        printf("Error: Could not flush the file stream.\n");
        fclose(file);
        return 1;
    }

    // Close the file
    fclose(file);

    return 0;
}

Output (in "example.txt"):

This is some text.

Real-World Use Case

Ensuring Immediate Log File Writes

In real-world applications, the fflush() function can be used to ensure that log messages are immediately written to a log file, which can be critical for debugging and monitoring purposes.

Example

#include <stdio.h>
#include <time.h>

void log_message(const char *message) {
    FILE *logfile = fopen("log.txt", "a");
    if (logfile == NULL) {
        printf("Error: Could not open log file.\n");
        return;
    }

    time_t now = time(NULL);
    fprintf(logfile, "%s: %s\n", ctime(&now), message);

    // Flush the log file to ensure the message is written immediately
    if (fflush(logfile) != 0) {
        printf("Error: Could not flush the log file.\n");
    }

    // Close the log file
    fclose(logfile);
}

int main() {
    log_message("Application started.");
    log_message("An event occurred.");

    return 0;
}

Output (in "log.txt"):

Wed Jul  4 12:34:56 2023: Application started.
Wed Jul  4 12:34:57 2023: An event occurred.

Conclusion

The fflush() function is essential for output stream management in C. It is used to clear the output buffer of a stream, ensuring that all data in the buffer is written to the intended output. This is particularly useful when you want to make sure that all buffered output is sent to a file or display immediately.

Comments