Reputation: 1023
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
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, thefflush()
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, thefflush()
function performs this flushing action on all streams for which the behavior is defined above.
Upvotes: 2
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:
stdin
- input to your program, bufferedstdout
- output of your program, bufferedstderr
- output for error messages, unbufferedSo 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
Reputation: 53016
The best way to know what it does is the standard
7.21.5.2 The fflush function
Synopsis
#include <stdio.h> int fflush(FILE *stream);
Description
If
stream
points to an output stream or an update stream in which the most recent operation was not input, thefflush
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.If
stream
is a null pointer, thefflush
function performs this flushing action on all streams for which the behavior is defined above.Returns
- The
fflush
function sets the error indicator for the stream and returnsEOF
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
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
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