C programming stdio.h function - int fflush(FILE *stream)

www.ig‮editfi‬a.com

In C programming, the stdio.h header file provides a set of functions for performing input and output operations on streams of data. One of the functions in this header file is fflush(), which flushes the output buffer of a given file stream.

The fflush() function takes one argument:

int fflush(FILE *stream);

The argument, stream, is a pointer to a FILE object that represents the file stream to be flushed.

The fflush() function flushes the output buffer of the specified file stream. If stream is a null pointer, all open output streams are flushed.

Here's an example of how to use fflush() to flush the output buffer of a file stream:

#include <stdio.h>

int main() {
    FILE *fp;

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

    fprintf(fp, "Hello, world!\n");
    fflush(fp);

    fclose(fp);

    return 0;
}

In the above example, the fopen() function is used to open a file named "example.txt" in write mode. The fprintf() function is then used to write the string "Hello, world!\n" to the file stream. The fflush() function is called to flush the output buffer of the file stream, ensuring that the data is actually written to the file on disk. Finally, the fclose() function is called to close the file stream.