Mail Archives: djgpp/2000/10/16/17:57:52
> From: Ben Pfaff <pfaffben AT msu DOT edu>
> Newsgroups: alt.comp.lang.learn.c-c++,comp.os.msdos.djgpp,comp.programming
> Date: 16 Oct 2000 17:13:55 -0400
>
> Damian Yerrick <Bullcr_pd_yerrick AT hotmail DOT comRemoveBullcr_p> writes:
>
> > For the record, what's the "right" way to clear an input buffer?
>
> Here's one "right" way, assuming that you mean "skip input up to
> the end of the line":
>
> for (;;) {
> int c = getc (stream);
> if (c == '\n' || c == EOF)
> break;
> }
IMHO, this is not a very good method: depending on system-dependent
buffering, this could become stuck inside the call to getc.
It is much better, in my experience, to simply fseek the stdin stream
to a sufficiently large negative offset. All implementations I've
seen will throw away buffered characters in this case.
Termios has a special function tcflush to discard characters pending
on a given file handle. This doesn't empty the characters buffered
inside a FILE object, though, but you could use tcflush in addition to
the fseek trick, to really empty everything.
- Raw text -