fflush(stdin)

This is a discussion on fflush(stdin) within the C Programming forums, part of the General Programming Boards category; I am as I said before studying C as a student and I keep hearing fflush(stdin) is wrong. I think ...

  1. #1
    Maybe? Maybe not...
    Join Date
    Feb 2005
    Location
    Atlanta
    Posts
    16

    Question fflush(stdin)

    I am as I said before studying C as a student and I keep hearing fflush(stdin) is wrong. I think I understand why and I have found posts explaining why fflush(stdin) is wrong. The instructor is the one who told us to use it as well as the book but I am aware that some of the books show methods of doing things that work but not in all cases and aren't the best way to do it only the simplest.

    The question is after searching the forum I have found very little that points me towards how Im supposed to clear the buffer manually. I really would rather not see source on how its done either I would rather learn it on my own than copy someone elses code. Can anyone either point me towards something that might give better clues or what exactly needs to be done to clear the input buffer? I would ask for functions but that kind of defeats the point in not looking at other peoples code.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    This is in the FAQ's i know you said you dont want to see code, but the explation is there

    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    When no one helps you out. Call google();

  3. #3
    Maybe? Maybe not...
    Join Date
    Feb 2005
    Location
    Atlanta
    Posts
    16
    Code:
    #include <stdio.h> 
     
    int main(void)
    {
    int ch;
    char buf[BUFSIZ];
     
    puts("Flushing input");
     
    while ((ch = getchar()) != '\n' && ch != EOF);
     
    printf ("Enter some text: ");
     
    if (fgets(buf, sizeof(buf), stdin))
    {
    	printf ("You entered: %s", buf);
    }
     
    return 0;
    }
    stdin is a standard FILE* variable that points to the input stream normally used for keyboard input. The fflush() function is deemed to flush buffers. Put the two together and you have a method for clearing the input stream easily, right? WRONG!
    Alright I see the code and since I didnt find anything else might as well try to work backwards instead of assuming I know it and understand and leave it at that.

    Sorry for what seems like a pointless question but still Id rather look like an ass and learn it than screw it up later.

    From what I see it looks like the while loop I think would amount to:

    Code:
    while ((ch = getchar()) != '\n' && ch != EOF);
    {
    	 printf ("Enter some text: ");
     
    	 if (fgets(buf, sizeof(buf), stdin))
    	 {
    			 printf ("You entered: %s", buf);
    	 }
    }
    and contains all of those statements. I don't really get the ch = getchar() but it looks like that is used to assign any character into ch for testing.

    printf prompts the user to enter some text which goes to the buffer.

    then if(fgets(buf,sizeof(buf),stdin)) is testing for a clear buffer and if it doesn't find one it reads it into buf. Then if this input is clean it the buffer is clear if it isn't and fgets doesnt see the null or newline it appends a newline character and the buffer is cleared correctly. Is this close to correct?

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    Code:
    while ((ch = getchar()) != '\n' && ch != EOF);
    That line alone is what flushes stdin.

    getchar() gets the next availible character from stdin, so you just keep getting the next character until you've reach a newline or nothing more is availible.

    The rest of the code is just to demonstrate that stdin was flushed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. fread/fwrite
    By chopficaro in forum C Programming
    Replies: 6
    Last Post: 05-11-2008, 01:48 AM
  3. Searching Into Files
    By St0rM-MaN in forum C Programming
    Replies: 12
    Last Post: 04-26-2007, 09:02 AM
  4. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 11:13 AM
  5. Linked List Need Help Urgent
    By ykchua in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21