Thread: Clearing stdin

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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