Bill said:
Where is the right place to put fflush in a file IO operation? Right
before fclose? I never have really used it to flush buffers only to flush
stdout. Is it a good idea to use fflush all the time?
I primarily use fflush() when debugging. When a program is run in Bash
like this:
program >out 2>&1
the only reliable way to get the output to come out in the right order
is to flush after the last of each separate cluster of fprintf(stderr,)
and fprintf(stdout,). Also in a case like this:
valgrind program >out 2>&1
valgrind will send output to stderr, and the program (generally) to
stdout. The order in the output file may be shifted with respect to
each other unless each fprintf(stdout,) is followed by fflush(stdout).
In a normally running program, which only writes to stdout, there is
generally no reason to use it. If a program runs a long time, it is
convenient to fflush once and a while, so that a second program can more
reliably look at the output while the first is still running. That is a
separate problem though, really, which concerns running two programs at
the same time, and passing data between them.
Regards,
David Mathog