Flush Input Buffer

This is a discussion on Flush Input Buffer within the C Programming forums, part of the General Programming Boards category; Code: inline bool Flush_Stream(FILE* stream) { void* p = malloc(BUFSIZ); if (p == NULL) { return false; } else setbuf(stream, ...

  1. #16
    Destoryer of Worlds
    Join Date
    Jul 2005
    Posts
    17
    Code:
    inline bool Flush_Stream(FILE* stream)
     {
        void* p = malloc(BUFSIZ);
        if (p == NULL)
            {
                return false;
            }
        else
            setbuf(stream, p);
          
        free(p);
        return true;
    }
    there we go that looks less insane :P
    sorry im anal about {} in if statements also you needed an else to be more clear what you wanted to happen in the if statement.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Which still doesn't fix the issue Salem last commented on. In any case, this function still won't do what the origional poster wanted it to, no matter what you do to it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Destoryer of Worlds
    Join Date
    Jul 2005
    Posts
    17
    I just read the man page for setbuf and yea your right that does not make alot of sense.


    lol well at least you got me to look at the man page now i know what setbuf does YAY !

  4. #19
    Registered User
    Join Date
    Oct 2005
    Posts
    7

    Question

    Code:
    void flush_stdin(void)
    {
    char ch = 0;
    
    while ((ch = getchar()) != '\n' && ch != EOF);
    }
    This code would always work if you could insert a char into stdin before the loop. Is this possible with any standard c function?

  5. #20
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    What an entertaining thread. I wonder when people will realize that if you do things right to begin with, there's no need to "flush" the input stream.

    >This code would always work if you could insert a char into stdin before the loop.
    Really? What if char is unsigned?
    My best code is written with the delete key.

  6. #21
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Ha...I'm kind of glad this thread got resurrected. It was fun to read the first time, and it's still a pretty good read now.
    Code:
    void flush_stdin(void)
    {
    char ch = 0;
    
    while ((ch = getchar()) != '\n' && ch != EOF);
    }
    I could have sworn that getchar() returns an int value and EOF is an int value.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #22
    Registered User
    Join Date
    Oct 2005
    Posts
    7
    Code:
    while ((ch = getchar()) != '\n' && ch != EOF);
    I took this loop from the faq on this forum, so I guess itīs correct... So do you know a function that inserts chars into stdin?

  8. #23
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I took this loop from the faq on this forum, so I guess itīs correct...
    That part is correct, but your little function isn't. Maybe you should read the FAQ more closely. Or better yet, declare ch as unsigned char and see what happens when you assume that getchar returns char.

    >So do you know a function that inserts chars into stdin?
    Not the way you're thinking of it.
    My best code is written with the delete key.

  9. #24
    Registered User
    Join Date
    Oct 2005
    Posts
    7
    how would you flush stdin?

  10. #25
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Yes I agree this is an entertaining thread What everyone failed to mention was that setbuf() must be called immediately after the stream is opened and before any i/o takes place. That pretty much makes the function unusable for stdin unless called at the start of the program in main(). passing NULL to setbuf() makes the stream unbuffered.

  11. #26
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    There are platform specific methods to rid the input stream of any pending data. Certain unix-like systems provide fpurge (and __fpurge on linux) to do this. As you've discovered, there's no completely portable.

    Also, as was said above, there's never any point to doing this anyway. If a user has put stuff on the input stream, that user will expect it to be dealt with, right? In a text based interactive program, you are prompting a user for input. The valid way to deal with "unwanted" cruft on the stream is to parse it and produce an error if appropriate.

    If you start trying to "flush" the input stream, you get into deep doodoo when the ambitious user tries to automate your program by piping pre-cooked data into your application. In short, your program becomes far less useful.

  12. #27
    Registered User
    Join Date
    Oct 2005
    Posts
    7
    But if I use scanf it can always happen that there will be chars left in stdin. Can could I prevent this with scanf or do I have to use another function?

  13. #28
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How could I prevent this with scanf?
    Don't mix scanf with other input methods. Use scanf intelligently and you'll have no problems. But an accepted solution is not to use scanf at all in favor of a combination of fgets to read a line and sscanf to parse it.
    My best code is written with the delete key.

  14. #29
    Registered User
    Join Date
    Oct 2005
    Posts
    7
    Ok, thanks now I understood it.

  15. #30
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    Quote Originally Posted by zx-1
    Since fflush() is illegal, and rewind() is not guaranteed to work, ...
    what means "fflush() is illegal"? you shouldn't use it anymore?
    I normally use fflush(stdin) after fgets()...

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Leaving charscters in input buffer
    By pavlosgr in forum C Programming
    Replies: 5
    Last Post: 11-24-2008, 01:10 PM
  2. input buffer settings
    By valaris in forum Linux Programming
    Replies: 6
    Last Post: 09-10-2008, 04:04 AM
  3. Flushing the input buffer
    By sand_man in forum C Programming
    Replies: 4
    Last Post: 09-15-2005, 05:23 PM
  4. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 04:15 AM

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