Mail Archives: djgpp/2000/10/17/02:15:20
Ben Pfaff wrote:
>
> 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;
> }
And here's another "right" way, making the same assumption, but also
assuming you don't like forever loops:
int clearstream(FILE *stream)
{
int eof_found = 0;
int ch;
while((ch=getc(stream)) != '\n' && ch != EOF)
{
continue;
}
if(ch == EOF)
{
eof_found = 1;
}
return eof_found;
}
This is not a superior technique to Ben's (in fact, it's the /same/
technique, but structured a little differently, and returning some
useful information on the way).
--
Richard Heathfield
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
66 K&R Answers: http://users.powernet.co.uk/eton/kandr2/index.html (31
to go)
- Raw text -