[comp.lang.c] flushing input buffer

boogaard@fys.ruu.nl (Martin vdBoogaard) (04/26/91)

Some of my programs use gets to read lines from stdin. Suppose that
(1) processing any of these lines takes a program some time and (2) the
user has already entered the second line of input when the program discovers
an error in the first line.

When this 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.

Does anyone have a portable clue?


Martin J. van den Boogaard                 |        boogaard@fys.ruu.nl
-----------------------------------------------------------------------
Dept. of Atomic & Interface Physics        |            the Netherlands
Debye Institute--Utrecht University        |        phone +31 30 532904
P.O. Box 80.000, NL-3508 TA Utrecht        |        fax   +31 30 543165
-----------------------------------------------------------------------
some days you wake up and immediately start to worry. nothing in
particular is wrong, it's just the suspicion that forces are aligning
quietly and there will be trouble.            --           Jenny Holzer
-----------------------------------------------------------------------

torek@elf.ee.lbl.gov (Chris Torek) (04/27/91)

In article <1991Apr26.152944.1928@fys.ruu.nl> boogaard@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@ee.lbl.gov

enag@ifi.uio.no (Erik Naggum) (04/27/91)

In article <1991Apr26.152944.1928@fys.ruu.nl>, Martin vdBoogaard writes:

   Some of my programs use gets to read lines from stdin. Suppose that
   (1) processing any of these lines takes a program some time and (2)
   the user has already entered the second line of input when the
   program discovers an error in the first line.

gets has the troubling quality that it's able to overwrite the input
buffer without telling you.  Most of the time, you don't want this to
happen.  Use fgets, instead.

   When this 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.

   Does anyone have a portable clue?

This is operating system dependent, since the program has no control
over what it has not yet read from the operating system/environment.
Portable in this context must mean a portable interface to a system-
dependent module, of which there is one version per operating system/
environment.

Under UNIX systems, you can issue an I/O control request to flush
input and output buffers in the operating system.  These are very much
different from I/O buffers in the program.

The appropriate I/O control for the UNIX system I'm sitting at right
now is

	ioctl (fd, TCFLSH, TCIFLUSH);

Don't think that this is what's good for your system.  Look up the
manual page for ioctl(2), follow the reference to what looks like the
terminal device (termio(4)), and look for the word "flush".  If you're
unfamiliar with ioctl's, read the entire ioctl man-page, and then read
all of the termio man-page, even if you don't (think you) need it
right now.

If you don't use a UNIX related system, it's even more important that
you isolate the specifics from the program proper, as it's very likely
to be hairy and gross.

--
[Erik Naggum]   	Professional programmer	     <enag@ifi.uio.no>
Naggum Software, Oslo, Norway			   <erik@naggum.uu.no>

s64421@zeus.usq.EDU.AU (house ron) (05/01/91)

enag@ifi.uio.no (Erik Naggum) writes:

>Under UNIX systems, you can issue an I/O control request to flush
>input and output buffers in the operating system.  These are very much
>different from I/O buffers in the program.

>The appropriate I/O control for the UNIX system I'm sitting at right
>now is

>	ioctl (fd, TCFLSH, TCIFLUSH);

>Don't think that this is what's good for your system.  Look up the
>manual page for ioctl(2), follow the reference to what looks like the
>terminal device (termio(4)), and look for the word "flush".  If you're
>unfamiliar with ioctl's, read the entire ioctl man-page, and then read
>all of the termio man-page, even if you don't (think you) need it
>right now.

I had occasion to do this under Unix.  As far as I could tell, these
ioctl calls are not reliable.  I don't have the details handy, though.

--
Regards,

Ron House.   (s64421@zeus.usq.edu.au)
(By post: Info Tech, U.C.S.Q. Toowoomba. Australia. 4350)