Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #9835 > unrolled thread

fflush

Started by"Bill Cunningham" <nospam@nspam.invalid>
First post2011-09-01 15:57 -0400
Last post2011-09-09 08:19 +0200
Articles 7 on this page of 27 — 16 participants

Back to article view | Back to comp.lang.c


Contents

  fflush "Bill Cunningham" <nospam@nspam.invalid> - 2011-09-01 15:57 -0400
    Re: fflush Willem <willem@toad.stack.nl> - 2011-09-01 20:09 +0000
    Re: fflush John Gordon <gordon@panix.com> - 2011-09-01 20:23 +0000
      Re: fflush sandeep <nospam@nospam.com> - 2011-09-01 20:49 +0000
        Re: fflush Kenneth Brody <kenbrody@spamcop.net> - 2011-09-02 10:59 -0400
      Re: fflush Kleuskes & Moos <kleuske@somewhere.else.net> - 2011-09-01 22:06 +0000
        Re: fflush Kenneth Brody <kenbrody@spamcop.net> - 2011-09-02 11:05 -0400
          Re: fflush Kleuskes & Moos <kleuske@somewhere.else.net> - 2011-09-02 17:04 +0000
    Re: fflush Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-09-01 14:55 -0600
    Re: fflush pete <pfiland@mindspring.com> - 2011-09-01 19:14 -0400
      Re: fflush Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-09-01 17:27 -0600
        Re: fflush pete <pfiland@mindspring.com> - 2011-09-01 19:33 -0400
        Re: fflush Seebs <usenet-nospam@seebs.net> - 2011-09-02 01:36 +0000
        Re: fflush Kenneth Brody <kenbrody@spamcop.net> - 2011-09-02 11:08 -0400
          Re: fflush Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-09-02 09:35 -0600
          Re: fflush Nobody <nobody@nowhere.com> - 2011-09-03 07:24 +0100
        Re: fflush gordonb.47ba6@burditt.org (Gordon Burditt) - 2011-09-02 21:42 -0500
          Re: fflush Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-09-02 21:01 -0600
    Re: fflush Nobody <nobody@nowhere.com> - 2011-09-02 00:34 +0100
    Re: fflush mathog <dmathog@gmail.com> - 2011-09-02 09:57 -0700
      Re: fflush David Thompson <dave.thompson2@verizon.net> - 2011-09-09 01:28 -0400
    Re: fflush gordonb.o3gsp@burditt.org (Gordon Burditt) - 2011-09-02 21:36 -0500
      Re: fflush Keith Thompson <kst-u@mib.org> - 2011-09-03 12:05 -0700
        Re: fflush Kenneth Brody <kenbrody@spamcop.net> - 2011-09-06 12:51 -0400
          Re: fflush Keith Thompson <kst-u@mib.org> - 2011-09-06 10:51 -0700
    Re: fflush "io_x" <a@b.c.invalid> - 2011-09-03 10:13 +0200
      Re: fflush "io_x" <a@b.c.invalid> - 2011-09-09 08:19 +0200

Page 2 of 2 — ← Prev page 1 [2]


#10043

FromDavid Thompson <dave.thompson2@verizon.net>
Date2011-09-09 01:28 -0400
Message-ID<86lg675va2sirppaccsr4rqjv828kdigfm@4ax.com>
In reply to#9898
On Fri, 02 Sep 2011 09:57:49 -0700, mathog <dmathog@gmail.com> wrote:

> Bill Cunningham wrote:
> >      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:
> 
fprintf or other output operations like fputc/putchar, fwrite, and of
course (!) the wide versions where applicable.

>    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).
> 
Alternatively, setvbuf stdout to _IOLBF once at the top of main(),
perhaps conditional on a debugging flag. This avoids missing one
somewhere, especially if you call library code. Also makes the output
nicer (though less exactly sequenced) if any of your output chunks is
not complete lines -- which is usually a bad idea anyway. 
It is a little slower if you (frequently) do multi-line chunks; but
IME valgrind is already so much slower you won't notice.

<snip rest>

[toc] | [prev] | [next] | [standalone]


#9902

Fromgordonb.o3gsp@burditt.org (Gordon Burditt)
Date2011-09-02 21:36 -0500
Message-ID<w4KdnWM2SZE4D_zTnZ2dnUVZ_uednZ2d@posted.internetamerica>
In reply to#9835
>    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?

stdout is not a special case here.  stdout has buffers, too.  Even
in default operation, stdout may buffer output and output a whole
line at a time.  stdout  may be directed to a file.  It may be
directed to a pipe, as in:

	myprog | tee logfile

Use fflush(stdout) if you have output a prompt (particularly if it
does not end in a newline, but also if it doesn't) and are going
to request input, so that the user can see the prompt *before*
providing the input.  (The same applies to streams that may be
connected to network connections, like SMTP or NNTP:  force the
command out of the buffers before waiting for a response.)

It is not necessary to fflush() before fclose().  fclose() flushes
buffers if necessary (the file was open for output and there's
something in the buffers to flush).

Do not call fflush() on input files or stdin.  Forcing the user to
untype input is a safety hazard, especially if the user is not at
the keyboard and is driving or asleep.  There's also a remote chance
it could damage the file.  fflush() does not mean "take the characters
in the buffer and *discard* them".  It means "take the characters in
the buffer and put them in the file."

Use fflush() on an output file if there is a possibility that someone
will be watching the file output (with, say, "tail -f"), the program
runs for a long time (e.g. hours, weeks, or "forever" as a daemon),
and output volume is low.  You might want to fflush() after each
set of error/log messages, rather than waiting a few days for the
buffer to fill up enough to flush on its own.

"progress messages" (e.g. "XX% complete") probably need a fflush()
after each one is output, as seeing them all at once on program
completion rather defeats their purpose.  (This applies to both
stdout and "log files").

Use fflush() to save a set of results of a long-running computations
(e.g. hours or weeks) so if the system crashes, completed results
are hopefully saved to disk.

Use fflush() frequently for debugging if the program is crashing
and you think you are missing output from parts of the program
executed before the actual point of the crash.

Writing most data files (as distinguished from "log files", although
the difference can be fuzzy) doesn't require fflush() at all.
Frequent use of fflush() in a loop has a possible performance
penalty.  Usually, it's minor, and likely unnoticable.  Occasionally,
this can result in really horrible performance, such as copying a
floppy one character at a time.  (I've seen this extend copy time
from a minute to an hour.)

[toc] | [prev] | [next] | [standalone]


#9917

FromKeith Thompson <kst-u@mib.org>
Date2011-09-03 12:05 -0700
Message-ID<lnfwkdqwej.fsf@nuthaus.mib.org>
In reply to#9902
gordonb.o3gsp@burditt.org (Gordon Burditt) writes:
[...]
> Do not call fflush() on input files or stdin.  Forcing the user to
> untype input is a safety hazard, especially if the user is not at
> the keyboard and is driving or asleep.  There's also a remote chance
> it could damage the file.  fflush() does not mean "take the characters
> in the buffer and *discard* them".  It means "take the characters in
> the buffer and put them in the file."

fflush() on an input file doesn't mean *anything*.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#9966

FromKenneth Brody <kenbrody@spamcop.net>
Date2011-09-06 12:51 -0400
Message-ID<x9ydnTa62KUb0vvTnZ2dnUVZ_gudnZ2d@bestweb.net>
In reply to#9917
On 9/3/2011 3:05 PM, Keith Thompson wrote:
> gordonb.o3gsp@burditt.org (Gordon Burditt) writes:
> [...]
>> Do not call fflush() on input files or stdin.  Forcing the user to
>> untype input is a safety hazard, especially if the user is not at
>> the keyboard and is driving or asleep.  There's also a remote chance
>> it could damage the file.  fflush() does not mean "take the characters
>> in the buffer and *discard* them".  It means "take the characters in
>> the buffer and put them in the file."
>
> fflush() on an input file doesn't mean *anything*.

Nit:  Some implementations define it to mean "discard anything already read 
do to buffered I/O.

But, as far as the C language itself is concerned, you are correct that it 
doesn't mean anything.  In fact, it's a bit more than "doesn't mean 
anything", as it's actually defined as being "undefined" (7.19.5.2p2).

-- 
Kenneth Brody

[toc] | [prev] | [next] | [standalone]


#9967

FromKeith Thompson <kst-u@mib.org>
Date2011-09-06 10:51 -0700
Message-ID<lnty8ppnjr.fsf@nuthaus.mib.org>
In reply to#9966
Kenneth Brody <kenbrody@spamcop.net> writes:
> On 9/3/2011 3:05 PM, Keith Thompson wrote:
>> gordonb.o3gsp@burditt.org (Gordon Burditt) writes:
>> [...]
>>> Do not call fflush() on input files or stdin.  Forcing the user to
>>> untype input is a safety hazard, especially if the user is not at
>>> the keyboard and is driving or asleep.  There's also a remote chance
>>> it could damage the file.  fflush() does not mean "take the characters
>>> in the buffer and *discard* them".  It means "take the characters in
>>> the buffer and put them in the file."
>>
>> fflush() on an input file doesn't mean *anything*.
>
> Nit:  Some implementations define it to mean "discard anything already read 
> do to buffered I/O.

Yes.

> But, as far as the C language itself is concerned, you are correct that it 
> doesn't mean anything.  In fact, it's a bit more than "doesn't mean 
> anything", as it's actually defined as being "undefined" (7.19.5.2p2).

Which is exactly equivalent to not being defined at all (4p2).

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#9907

From"io_x" <a@b.c.invalid>
Date2011-09-03 10:13 +0200
Message-ID<4e61e102$0$44209$4fafbaef@reader1.news.tin.it>
In reply to#9835
"Bill Cunningham" <nospam@nspam.invalid> ha scritto nel messaggio
news:4e5fe3b1$0$27973$bbae4d71@news.suddenlink.net...
>    Where is the right place to put fflush in a file IO operation? Right
in input/output

where who answer have to send data
eg.
input to the pc program
when the program ask a question afther printf(question);fflush(stdout);

or input to the internet client etc
etc

> 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?
>
> Bill
>
>


[toc] | [prev] | [next] | [standalone]


#10044

From"io_x" <a@b.c.invalid>
Date2011-09-09 08:19 +0200
Message-ID<4e69af70$0$15665$4fafbaef@reader2.news.tin.it>
In reply to#9907
"io_x" <a@b.c.invalid> ha scritto nel messaggio 
news:4e61e102$0$44209$4fafbaef@reader1.news.tin.it...
>
> "Bill Cunningham" <nospam@nspam.invalid> ha scritto nel messaggio
> news:4e5fe3b1$0$27973$bbae4d71@news.suddenlink.net...
>>    Where is the right place to put fflush in a file IO operation? Right
> in input/output
>
> where who answer have to send data
> eg.
> input to the pc program
> when the program ask a question afther printf(question);fflush(stdout);
>
> or input to the internet client etc
> etc

possible i never explain above well.
i not follow the C standard when flush, if "\n" is meet.
one has to flush only when
1) buffer is full
2) when flush is call

>> 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?
>>
>> Bill
>>
>>
>
>
> 

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.c


csiph-web