jDoe
jDoe

Reputation: 1023

the use of fflush(FILE* stream)

I Really can't understand the use of the : fflush() function , could I find a good implementation of this function ? I read some resources about it but I still can't grasp it well , actually I want to know what this function really do ?

Upvotes: 2

Views: 4446

Answers (5)

Natasha Dutta
Natasha Dutta

Reputation: 3272

Quoting C11 standard, chapter 7.21.5.2

int fflush(FILE *stream);

If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush() function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

So, basically, the fflush(stream) call will forcefully (even if the buffer is not full) flush out any data that is present in the buffer associated with that particular stream to the output file (disk).

Also, as a note, from paragraph 3, same document

If stream is a null pointer, the fflush() function performs this flushing action on all streams for which the behavior is defined above.

Upvotes: 2

ZbyszekKr
ZbyszekKr

Reputation: 512

The streams can be buffered. That mean's that after using a function like printf or fprintf the characters you send don't go immediately. Instead they wait for proper situation to be sent.

This can creates some problems because It can sometimes take a long time for the stream the finally come through.

For that reason there is a command fflush(FILE *stream) which forces the stream.

In Unix system a program always has 3 file descriptors open:

  1. stdin - input to your program, buffered
  2. stdout - output of your program, buffered
  3. stderr - output for error messages, unbuffered

So when you are using descriptor stdout ( printf does that) and you want to be shure that the characters show up right ahead, use fflush.

Upvotes: 1

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53016

The best way to know what it does is the standard

7.21.5.2 The fflush function

Synopsis


  1. #include <stdio.h>
    int fflush(FILE *stream);
    

Description

  1. If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.

  2. If stream is a null pointer, the fflush function performs this flushing action on all streams for which the behavior is defined above.

Returns

  1. The fflush function sets the error indicator for the stream and returns EOF if a write error occurs, otherwise it returns zero.

So basically the the library I/O functions use buffers, fflush() moves the data from the buffers to the physical file, that's why fflush(stdin) as you can understand from above is undefined behavior, because fflush() is only defined for output streams.

The above means, that write operations on output streams are not performed immediately, they are buffered, when you call fflush() then they are flushed to the disk, you don't need to fflush() explicitly all the time, for example in

fpritnf(stdout, "Example text\n");

the "\n" at the end of the printed text, triggers a flush automatically, also when you call fclose() or exit the program the buffers are flushed.

You may also notice, that even for output streams the behavior is not defined unless the most recent operation is output, input operations cannot be flushed.

Upvotes: 2

Some programmer dude
Some programmer dude

Reputation: 409442

When you write to a FILE* using e.g. fprintf (or even printf which is about equal to fprintf(stdout, ...)) the data is normally stored in a buffer. When the buffer is full it is flushed (written) to the actual output file.

If you want to write the contents of the buffer to the file before it is full, you can call fflush which does just that.

Upvotes: 1

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215547

fflush is needed when you need to control when output written to a stdio FILE actually becomes visible, since stdio normally buffers output and writes a large chunk as a unit. For basic C programs not using any interfaces with the operating system outside of the C standard library, the main/only time you need this kind of control is when the output is being shown on an interactive device (screen/terminal) and you want to make sure it's physically visible there. On unix-like/POSIX systems and in other environments with multiprocessing, though, you may also want to ensure that data is written out to files or pipes that other processes might need to read.

Upvotes: 1

Related Questions