Message-ID: <39EBEBF3.2A5EDE06@eton.powernet.co.uk> Date: Tue, 17 Oct 2000 07:04:35 +0100 From: Richard Heathfield Organization: Eton Computer Systems Ltd X-Mailer: Mozilla 4.6 [en-gb]C-CCK-MCD NetscapeOnline.co.uk (WinNT; I) X-Accept-Language: en-GB,en MIME-Version: 1.0 Newsgroups: alt.comp.lang.learn.c-c++,comp.os.msdos.djgpp,comp.programming Subject: Re: Undertaking a programming journey References: <8scg36$gsm$1 AT nnrp1 DOT deja DOT com> <39E9CF07 DOT 785C0C0F AT eton DOT powernet DOT co DOT uk> <8scls9$kth$1 AT nnrp1 DOT deja DOT com> <39E9FAD5 DOT DE1FDAE4 AT eton DOT powernet DOT co DOT uk> <8sdrub$h7u$1 AT nnrp1 DOT deja DOT com> <39EAA40B DOT 31B0CA89 AT eton DOT powernet DOT co DOT uk> <8seoli$65v$1 AT nnrp1 DOT deja DOT com> <39EAF73E DOT ECA52E1A AT antlimited DOT com> <8sfbu7$n06$1 AT nnrp1 DOT deja DOT com> <39EB4271 DOT 85CE6874 AT antlimited DOT com> <8sfhr4$si2$1 AT nnrp1 DOT deja DOT com> <87og0k5w18 DOT fsf AT pfaffben DOT user DOT msu DOT edu> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 195.60.5.117 X-Trace: 17 Oct 2000 07:08:46 +0100, 195.60.5.117 Lines: 44 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Ben Pfaff wrote: > > Damian Yerrick 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)