fflush(stdin) (was: Re: learning c)

Steve Summit scs at adam.pika.mit.edu
Sat Apr 8 14:19:36 AEST 1989


In article <1383 at auspex.auspex.com> guy at auspex.auspex.com (Guy
Harris) correctly points out that:
>..."fflush" doesn't
>flush input, so the ANSI standard says nothing new - it says "it flushes
>output, we make no claims about input", which reflects current practice
>(S5R3's "fflush" appears to flush input, although it's not documented -
>which is kind of obnoxious; either it's a useful feature, in which case
>it should be documented and supported, or it's a useless one, in which
>case it shouldn't have been put in...

(Rahul Dhesi made essentially the same point earlier.)

The trouble with fflush on an input stream is that it isn't
analogous to fflush on an output stream.  fflush on an output
stream means "do the write right now;" however, no equivalent
interpretation is meaningful for an input stream.  Packages that
I know of that do allow fflush on an input stream instead discard
the contents of the buffer, which is very different from what
fflush on an output stream does.

I'm contemplating adding fabort(FILE *) to my stdio library--
this would discard the buffer contents from an input or output
stream, without doing any I/O.  For an input stream, this would
be equivalent to what those extended fflush'es apparently do, but
it avoids the nonportable overloading, while admitting the
possibility of an orthogonal operation on an output stream.  Once
upon a time I wanted such a thing for an output stream, perhaps
for use in a control-C handler.  (I can't remember now; fabort
probably wouldn't have helped anyway.)

Comments?

                                            Steve Summit
                                            scs at adam.pika.mit.edu



More information about the Comp.lang.c mailing list