fseek fread fwrite fflush

Stephen Clamage steve at taumet.com
Mon Apr 15 03:10:28 AEST 1991


tmurphy%peruvian.utah.edu at cs.utah.edu (Thomas Murphy) writes:

>In article <1991Apr13.003817.12434 at watdragon.waterloo.edu> amewalduck at trillium.waterloo.edu (Andrew Walduck) writes:
>>I've got a question about fseek, fread, fwrite and fflush....
>>Is it necessary to do the following?
>>fflush(fp);
>>fseek(fp....);
>>fwrite(fp...);

>You are always safest if you flush streams btwn io phases...this goes
>for io to the console or to some file...thus if you have been reading
>you should flush before writing and again before returning to reading
>again and in addition flush before closing...

Not quite true.  The ANSI rules state that the effect of fflush() on
a file whose last operation was a read are undefined -- an implementation
is allowed to do bad things if you fflush() after an input operation.
But you are guaranteed to be able to safely switch between reading
and writing a file if you fseek() first -- even fseek() to the same
position.

For non-ANSI systems, you have to check the manual to see whether the
fflush() is safe or required.
-- 

Steve Clamage, TauMetric Corp, steve at taumet.com



More information about the Comp.lang.c mailing list