Re: Flushing an input stream

From: Alex Vinokur (alexvn_at_big-foot.com)
Date: 08/10/04


Date: Tue, 10 Aug 2004 16:48:52 +0300


<zerotyNoSpamForMepe@yahoo.com> wrote in message news:frihh0ho6ji100u49nov9ifo1icjt2ugk1@4ax.com...
> Hi,
>
> I've recently replaced a lot of uses of cin>> with cin.get and the
> like, and I'd like to know if someone can answer a question that was
> preying on my mind before I did so. I had previously handled the
> problem of the program going beserk when a string with a space, (or
> the wrong kind of data, or too much data) was entered by using
> fflush(stdin). Although this worked in Borland C++, I have been told
> it doesn't usually. Is this correct, and if so, how can I flush the
> junk from the input stream?
>
> Thanks,
>
> James McLaughlin.

<QUOTE from "comp.lang.c Answers (Abridged) to Frequently Asked Questions (FAQ)"
at http://groups.google.com/groups?selm=2004Aug01.0600.scs.0005%40eskimo.com >

12.26a: Will fflush(stdin) flush unread characters from the standard
 input stream?

A: No.

12.26b: If fflush() won't work, what can I use to flush input?

A: It depends on what you're trying to do; see the full list for
 details. (But first see question 12.20.)

</QUOTE>

<QUOTE from "comp.lang.c Answers to Frequently Asked Questions (FAQ List)"
at http://groups.google.com/groups?selm=2004Aug01.0600.scs.0001%40eskimo.com >

12.26a: How can I flush pending input so that a user's typeahead isn't
 read at the next prompt? Will fflush(stdin) work?

A: fflush() is defined only for output streams. Since its
 definition of "flush" is to complete the writing of buffered
 characters (not to discard them), discarding unread input would
 not be an analogous meaning for fflush on input streams.
 See also question 12.26b.

 References: ISO Sec. 7.9.5.2; H&S Sec. 15.2.

12.26b: If fflush() won't work, what can I use to flush input?

A: It depends on what you're trying to do. If you're trying to get
 rid of an unread newline or other unexpected input after calling
 scanf() (see questions 12.18a-12.19), you really need to rewrite
 or replace the call to scanf() (see question 12.20).
 Alternatively, you can consume the rest of a partially-read line
 with a simple code fragment like

  while((c = getchar()) != '\n' && c != EOF)
   /* discard */ ;

 (You may also be able to use the curses flushinp() function.)

 There is no standard way to discard unread characters from a
 stdio input stream, nor would such a way necessarily be
 sufficient, since unread characters can also accumulate in
 other, OS-level input buffers. If you're trying to actively
 discard typed-ahead input (perhaps in anticipation of issuing a
 critical prompt), you'll have to use a system-specific
 technique; see questions 19.1 and 19.2.

 References: ISO Sec. 7.9.5.2; H&S Sec. 15.2.

</QUOTE>

System-specific technique for UNIX is to use tcflush - flush non-transmitted output data, non-read input data or both.
See http://www.cs.queensu.ca/cgi-bin/local/man.cgi?section=3C&topic=tcflush.

Samples can be seen at:
http://groups.google.com/groups?selm=b5skqr%242d7uag%241%40ID-79865.news.dfncis.de
http://alexvn.freeservers.com/s1/download.html (Flushing non-read input data with using tcflush)

-- 
   Alex Vinokur
     http://mathforum.org/library/view/10978.html
     http://sourceforge.net/users/alexvn


Relevant Pages