05-17-05 12:48 PM
> I am using fprintf to write the output to "stdout".
> It works fine.
> However when I redirect the output to some file at the
> command prompt, the output in the file appears after few
> seconds. However, if put fflush after the fprintf it works
> fine.
That is a normal behaviour. The fprintf() function uses a buffer to let
the system choose when to write data. When calling fflush(), you're asking
the system to force all the data waiting to be written to ... be written,
"now" (of course, the system decides whether "now" is really right now,
or if the task must wait a little bit). :-)
There are various buffers within the system. If I understand correctly how
things work, you have a buffer for your data waiting to be written, but
you also have at least one more buffer for the disk writings. So when
you're outputing to the console, everything's fine, because the time you
have to wait is quite short (but if you were using multi-{task,thread}
programming, this probably wouldn't be so), whereas with the disk, you
have a "physical writing" to be performed in addition to the "normal" one.
--
"Je deteste les ordinateurs : ils font toujours ce que je dis, jamais ce
que je veux !"
"The obvious mathematical breakthrough would be development of an easy
way to factor large prime numbers." (Bill Gates, The Road Ahead)
[ Post a follow-up to this message ]
|