Thread: Clearing stdin

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    Clearing stdin

    HI there,

    Currently I am running a loop where fgets is used to read the first 80 characters into a string. This function is being called within a while loop that currently continues until all stdin is read ie if 160 characters are input it will repeat twice (thus printing the two strings), How would I go about clearing the stdin value once the first 80 characters are read? ie that the function will only print one of the strings. It is important to note that the condition of the while loop cannot be changed as it functions as the quit command (when the string returned =="quit" the lop exits).

    Thank-you for your time
    ~qwertyuiop23

    EDIT:: Here is a short example of what I am meaning:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int getstring()
    {
    	char buffer[10];
    	char *cp = fgets(buffer,10,stdin);
    	int quit = 0;
    	if (cp!=NULL) {
    		if (strncmp(buffer,"quit",4)==0) {
    			quit = 1;
    		} else {
    			quit = 0;
    		}
    	}
    	return quit;
    }
    
    int main()
    {
    	do {
    		printf("\nStill running:");
    	} while (getstring()!=1);
    	return 0;
    }
    Last edited by qwertyuiop23; 08-14-2011 at 07:29 PM.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Read this: FAQ: Clearing the input buffer. It gives you a couple of options with explanations.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    Thank-you. I did try writing something like this. However, putting it just before the return quit; part of the first function causes the program to what for enter to pressed one more time even though the quit command has been entering. This is because the /n character is appended to the end of the string read by fgets and hence does not exist in stdin to be read by getchar(); Any ideas on a work around?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I guess the question is: what do you actually want to happen? Are you trying to read only eighty characters? As many characters as are typed in? Neither?

    You can always use strlen to see how many characters were read in; if it's less than 79, then you know there's nothing left to read.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,533
    It would be helpful if you posted your code.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    strlen worked a treat. I just put this code in:

    Code:
    if (strlen(buffer)==9)
    {
    	while ((ch = getchar()) != '\n' && ch!=EOF);
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,968
    > strlen worked a treat. I just put this code in:
    Except if the user typed in "12345678\n", the buffer would be exactly full and there would be NO extra characters on the input stream.

    Check to see if your buffer has a \n before deciding to get rid of remaining characters.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Blocking or clearing stdin
    By woopitt in forum C Programming
    Replies: 4
    Last Post: 05-23-2011, 05:05 PM
  2. use of stdin
    By droseman in forum C Programming
    Replies: 13
    Last Post: 09-26-2008, 09:42 PM
  3. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  4. ascii 10 in the stdin after fflush(stdin)??
    By Kev2 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 06-03-2002, 03:53 PM
  5. stdin
    By ygfperson in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 09:59 AM