[comp.lang.c] Removing excess characters from input stream

scott@ncifcrf.gov (Michael Scott) (03/22/91)

I don't read comp.lang.c so when I recently had a problem in C I immediately 
got the FAQ for this news group and read it.   Unfortunately, the answer I 
wanted was not in there (this was a while back).  Well, I have the same 
problem again so I'll post the problem I have.

I am writing a program that has a lot of keyboard interaction.  So I wrote 
a hierarchy of routines to get data from the keyboard.

For example I have one routine called get_data() which can either get 
a 'char *' ,'float' or 'int' depending upon what arguments are passed.  
get_data() and another high level routine both call a lower routine named
get_string().  get_string() receives a string from the keyboard and the higher 
level routines coerce this string into the proper data type.  

The low level routine, get_string(), reads all input from the keyboard for the
program.  Three arguments are passed get_string() -- the string to hold the 
data, the length of the string, and the prompt to issue the user.

The problem is with excess characters typed in.  IF a user enters more than 
138 (137? I can't remember) characters then the characters over this amount
remain on the input stream.  For example, if the string which will hold the 
data is 30 characters and the user types in 140 characters then the string will
contain the first 30 characters.  The characters between 30 and 138 are 
discarded and the characters after 138 remain on the input stream.   What
I want to happen is that all characters after 30 will be dicarded and the 
input stream be empty.   

Because the characters remain on the input stream my next prompt is answered
by these characters -- not good.   I is very unlikely that anybody will enter
more than 138 characters on the keyboard -- but I would like the program to
be foolproof.  

I use fflush in get_string, even though the man page only refers to its use 
regarding output streams.  However, If I don't use fflush then no characters 
are discarded and I have all characters over 30 remaining on the input stream.

Also, I want to use code that is portable -- which rules out many system
calls.   This will be running on a PC.  But it may run on suns and vaxen.

The Question:  What can I do to rid the input stream of all unnecessary
               characters after the valid data is received.


Here's get_string()
 
void get_string(string,length,prompt)
 char *string; int length; char *prompt;

{
 fprintf(stdout,prompt);
 if ((fgets(string,length,stdin)) == (char *)NULL)
  {
   string[0] = '\0';
   fprintf(stdout,"\n");
   clearerr(stdin);
  }
 else
  string[strlen(string) -1 ] = '\0';  /* blow away the <cr> */
 fflush(stdin);
}

Please send all answer directly to me --  I will summarize to the newsgroup. 

If I have violated any comp.lang.c protocol then by all means flame me.

_____________________________________________________________________________

	Name:		Michael Scott
        Title:  	Sr. Systems Manager
	Phone #:	(301) 846-5670
	Company:	National Cancer Institute
	E-mail:		scott@ncifcrf.gov


-- 
_____________________________________________________________________________

	Name:		Michael Scott
        Title:  	Sr. Systems Manager