how do I clear stdin buffer

Brad Willcott bxw at ccadfa.adfa.oz.au
Wed Jun 19 00:20:05 AEST 1991


tms at 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 at ccadfa.cc.adfa.oz
Computing Services,                     Internet:   bxw at 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)



More information about the Comp.lang.c mailing list