tmurphy%peruvian.utah.edu@cs.utah.edu (Thomas Murphy) (04/13/91)
In article <1991Apr13.003817.12434@watdragon.waterloo.edu> amewalduck@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.....sounds like rules of the john eh? murph thomas murphy u of u tmurphy%peruvian@cs.utah.edu
amewalduck@trillium.waterloo.edu (Andrew Walduck) (04/13/91)
I've got a question about fseek, fread, fwrite and fflush.... Is it necessary to do the following? fflush(fp); fseek(fp....); fwrite(fp...); Or can I just do the following... fseek(fp....); fwrite(fp....); I seem to remember a discussion about this a long time ago, and am curious... Thanx Andrew Walduck
steve@taumet.com (Stephen Clamage) (04/15/91)
tmurphy%peruvian.utah.edu@cs.utah.edu (Thomas Murphy) writes: >In article <1991Apr13.003817.12434@watdragon.waterloo.edu> amewalduck@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@taumet.com
mike@bria.UUCP (Michael Stefanik) (04/15/91)
In an article, Thomas Murphy writes: |In an article, 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.....sounds like rules of |the john eh? This seems to have the same effect as knocking on wood, or throwing salt over one's shoulder -- if such superstitious fflush()'s make you feel better, then do so by all means. The only situation that I have found it to be useful is dumping the stream before I do a raw read on a tty. However, I doubt the merit of doing them in the name of "safety". Certainly, a fflush() before a close is redundant. -- Michael Stefanik, MGI Inc, Los Angeles | Opinions stated are never realistic Title of the week: Systems Engineer | UUCP: ...!uunet!bria!mike ------------------------------------------------------------------------------- If MS-DOS didn't exist, who would UNIX programmers have to make fun of?
berg@marvin.e17.physik.tu-muenchen.de (Stephen R. van den Berg) (04/15/91)
Andrew Walduck writes: >Is it necessary to do the following? >fflush(fp); >fseek(fp....); >fwrite(fp...); >Or can I just do the following... >fseek(fp....); >fwrite(fp....); According to the ANSI standard library, the latter can be practiced. Every fseek performs an implicit fflush. Though, extra fflushes won't hurt. -- Sincerely, berg@marvin.e17.physik.tu-muenchen.de Stephen R. van den Berg. "I code it in 5 min, optimize it in 90 min, because it's so well optimized: it runs in only 5 min. Actually, most of the time I optimize programs."
dhesi%cirrusl@oliveb.ATC.olivetti.com (Rahul Dhesi) (04/16/91)
Somebody writes: Certainly, a fflush() before a close is redundant. Peculiar counterexample follows. In an MS-DOS environment, to set the timestamp on a file, you must first open it and get a file descriptor (file handle) to it. So, if you have just finished writing data to an open file and want to leave its timestamp set to some special value, the safe sequence is: if (fflush(f) != 0) { handle error; } set_timestamp(fileno(f), time_value); (void) fclose(f); If the fflush is not done, the fclose can write data to the file, overriding the timestamp that has been set. -- Rahul Dhesi <dhesi@cirrus.COM> UUCP: oliveb!cirrusl!dhesi