flushing input buffer

Chris Torek torek at elf.ee.lbl.gov
Sat Apr 27 23:45:09 AEST 1991


In article <1991Apr26.152944.1928 at fys.ruu.nl> boogaard at fys.ruu.nl
(Martin vdBoogaard) writes:
>Some of my programs use gets to read lines from stdin.

(Switch to fgets.  We deliberately made the gets() function obnoxious
in the new BSD stdio.  For those without time or inclination to fix
their code, a quiet version of gets() appears in -lcompat.)

>When [an] error is detected, the program skips the rest of the
>offending input line (which is not likely to make much sense either).
>In addition I'd like to be able to skip any input the user has already
>entered up to the moment the program decides it must handle the error.
>I can't use gets because I don't know *how much* superfluous input I have
>to skip. The fflush function works only for *output* streams.

Correct.

>Does anyone have a portable clue?

There is no portable input flush routine.

The new BSD stdio has an `fpurge' function which `forgets' any pending
input or output in a stdio stream.  If the input is pending but not in
a stream, however, you must use system-specific code (ioctl(TIOCFLUSH),
e.g.).
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 415 486 5427)
Berkeley, CA		Domain:	torek at ee.lbl.gov



More information about the Comp.lang.c mailing list