Thread: Blocking or clearing stdin

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    2

    Blocking or clearing stdin

    Hey all. The solution to this might be very simple, but I can't figure it out.
    I was able to dumb my code down to this:

    Code:
    int main()
    {
    	char msg[512];
    	printf("Enter a message: ");
    	fgets(msg,sizeof msg, stdin);
    	printf("Entered: %s",msg);
    	printf("Hold on while we wait 3 seconds...\n");
    	usleep(3000000);
    	printf("Wait is over.\n");
    	printf("Enter another message: ");
    	fgets(msg,sizeof msg, stdin);
    	printf("Entered: %s",msg);
    	return 0;
    }
    Looks simple enough: read in a message, wait 3 seconds, read in another message.
    When I wait 3 seconds like I'm supposed to, everything works according to plan. But when I enter the first message, then during the 3 second pause enter several messages, something unexpected occurs. Even during usleep, things entered in the terminal get pushed onto stdin. Then the second message is interpreted as the very first thing entered AFTER the first message, in this case occurring during the 3 second pause:

    Enter a message: 1
    Entered: 1
    Hold on while we wait 3 seconds...
    2 (<--- now during the pause I flood the input with garbage that I want ignored)
    3
    4
    5
    Wait is over.
    Enter another message: Entered: 2
    -bash: 3: command not found
    -bash: 4: command not found
    -bash: 5: command not found

    My goal is that once the program gets to the second call to fgets(), it prompts the user for input and blocks until that input is given. Instead of this happening, it is immediately taking the first thing entered during the 3 second pause.

    So I either need a way of blocking stdin during the pause, or a way of clearing stdin immediately following the pause. I have tried fflush(stdin) and rewind(stdin).

    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    2
    Quote Originally Posted by rags_to_riches View Post
    My apologies. Still can't get it to work, though. That suggests clearing stdin by adding this:
    Code:
    char ch;
    while ((ch = getchar()) != '\n' && ch != EOF);
    However this only clears up to one line. I need everything cleared. I tried making it
    Code:
    while ((ch = getchar()) != EOF);
    but this creates an infinite loop, with EOF never being reached. What marks the end of stdin?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by woopitt View Post
    Code:
    while ((ch = getchar()) != EOF);
    but this creates an infinite loop, with EOF never being reached. What marks the end of stdin?
    stdin doesn't end... it's your keyboard buffer... while(getchar());

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The terminal device itself is buffering the input. This has nothing at all to do with your program.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. check stdin for piped data without blocking
    By HowardL in forum C# Programming
    Replies: 0
    Last Post: 10-19-2009, 09:16 AM
  2. Pros and Cons of blocking versus non-blocking sockets?
    By abachler in forum Networking/Device Communication
    Replies: 4
    Last Post: 05-08-2008, 06:52 AM
  3. Periodic non-blocking IO in stdin
    By blakeops in forum C Programming
    Replies: 4
    Last Post: 04-20-2007, 11:43 PM
  4. 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
  5. 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