Re: how truncate a file in C?

From: Joe Halpin (jhalpin_at_nortelnetworks.com_.nospam)
Date: 06/17/03


Date: 17 Jun 2003 09:16:53 -0500

Valentin Nechayev <netch@segfault.kiev.ua> writes:

> >>> Frank Cusack wrote:
>
> > MR>> True, but truncating a file opened as a FILE would have an unpredictable
> > MR>> effect on further I/O with the FILE.
> >> IMO fflush() before truncating and fseek() after it help from unpredictability.
> FC> fflush() guarantees nothing here.
>
> Citing SUSv3:
>
> ==={{{
> fflush() shall cause any unwritten data for that stream to be written
> to the file,
> ===}}}
>
> FC> You cannot mix stdio and system I/O
> FC> predictably, period.
>
> You didn't show any arguments for this words.
>
> FC> The effects of interchanging the calls are
> FC> system dependent,
>
> Where is it written? Please show.

SUS3 isn't quite as absolute as the above, but it does give some rules
for defined interaction between stdio streams and file descriptors,
see 'Interaction of File Descriptors and Standard I/O Streams" in
http://www.opengroup.org/onlinepubs/007904975/functions/xsh_chap02_05.html#tag_02_05

If these rules aren't followed, the results are unspecified.

Joe



Relevant Pages

  • Re: fflush(stdout);
    ... the fflush does not add anything. ... any terminal partial line on text streams. ... it releases the current contents of the output buffer to the system ... also trigger the necessary flushing. ...
    (comp.lang.c)
  • Re: switches
    ... fflushflushes output streams, and should have no effect on input ... fflush() on an input stream invokes undefined behavior. ...
    (comp.lang.c)
  • Re: C Interview Questions
    ... Richard Heathfield wrote: ... and stdout is only /one/ correct argument for fflush. ... I thought stdin, stdout, stderr where all macros to FILE * for the 3 streams. ... Now the standard does say for buffered streams, ...
    (comp.lang.c)
  • Re: C Interview Questions
    ... Simon Biber wrote: ... streams. ... which is another reason that there is no such thing as *the* correct ... argument to fflush. ...
    (comp.lang.c)
  • Re: _cleanup() vs Linux fcloseall()
    ... I'd prefer to see a function that closed all file descriptors greater ... than a specified limit (which makes it easy for a process to preserve ... stdin/out/err but close everything else). ... indirectly by saying that fflushis used prior to closing the streams. ...
    (freebsd-current)