Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!magnus.acs.ohio-state.edu!csn!ccncsu!purdue!haven!ncifcrf!fcs260h2!scott From: scott@ncifcrf.gov (Michael Scott) Newsgroups: comp.lang.c Subject: Summary: Removing excess characters from input stream Message-ID: <2097@fcs280s.ncifcrf.gov> Date: 22 Mar 91 12:46:00 GMT Sender: news@ncifcrf.gov Distribution: na Organization: NCI Supercomputer Facility, Frederick, MD Lines: 46 Here's a quick solution to my question regarding blowing away excess characters unneeded by a program. I was insistent thinking that there would be a library call to flush stdin. Well no one referred me to a library call that could do that job -- and I could not find one. This solution is from John Hascall. It's simple and it works!! > 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 #ifdef YOURS > string[strlen(string) -1 ] = '\0'; /* blow away the */ > fflush(stdin); #endif #ifdef MINE if (string[strlen(string) - 1] == '\n') { /* * got the newline, blow it away */ string[strlen(string) - 1] = '\0'; } else { /* * no newline, read 'till we find it */ do {} while (getc(stdin) != '\n'); } #endif > } -- _____________________________________________________________________________ Name: Michael Scott Title: Sr. Systems Manager