Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!munnari.oz.au!manuel!ccadfa!bxw From: bxw@ccadfa.adfa.oz.au (Brad Willcott) Newsgroups: comp.lang.c Subject: Re: how do I clear stdin buffer Message-ID: <2438@ccadfa.adfa.oz.au> Date: 18 Jun 91 14:20:05 GMT References: <43310@cup.portal.com> Organization: Computer Centre, University College, UNSW, ADFA, Canberra, Australia Lines: 43 tms@cup.portal.com (Taos Mountain Software) writes: >The stdio function "scanf" works great for input from datafiles, but I always >run into problems when doing interactive tty I/O on Unix systems. >For example, this simple program will go into an infinite loop >if the entry is a non-digit for example ("abc"). #define INT_SIZE 5 > main () > { > int x,stat; char buffer[INT_SIZE]; fflush (stdin); > do { fgets (buffer, INT_SIZE, stdin); stat = sscanf ("%d", &x); > /* stat=scanf("%d",&x); */ > } while (stat != 1); > ... > ... > } fflush (FILE *stream) { int i; while ((i = getc (stream)) != EOF) ; } >I know there are plenty of workarounds, but what I'm interested in is a >function like fflush that deals with input streams. -- Brad Willcott, ACSnet: bxw@ccadfa.cc.adfa.oz Computing Services, Internet: bxw@ccadfa.cc.adfa.oz.au Australian Defence Force Academy, UUCP:!uunet!munnari.oz.au!ccadfa.oz!bxw Northcott Dr. Campbell ACT Australia 2600 +61 6 268 8584 +61 6 268 8150 (Fax)