Thread: Flush Input Buffer

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    151

    Flush Input Buffer

    Since fflush() is illegal, and rewind() is not guaranteed to work, is this legal, and more importantly, function the way I'm intending it to?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    // Flush input buffer (actually, any buffer)
    //
    // Parameters:
    //    FILE* stream    Pointer to file to be 'debuffered'.
    // Returns:
    //    bool - false if error, true if successful.
    // Notes:
    //    For output streams, does not mimic fflush() in that remaining
    //    data is lost.
    inline bool Flush_Stream(FILE* stream)
    {
        void* p = malloc(BUFSIZ);
        if (p == NULL)
            return false;
    
        setbuf(stream, p);
        return true;
    }
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't believe so. At least, not if I'm understanding the man page correctly. Actually, there is no standard way you can have unbuffered input, so you don't even need to try.

    Wait a second. Flushing the input stream and unbuffering it are two different things. Unbuffering refers to making it non-buffered. Meaning, as soon as you press a key, it is sent immediately. (IE: Not buffered.)

    Flushing the stream simply means to clear out any pending input. That's a snap to do:
    Code:
    void fflushstdin( void )
    {
        int c;
        while( (c = fgetc( stdin )) != EOF && c != '\n' );
    }

    Quzah.
    Last edited by quzah; 07-20-2005 at 08:39 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Quote Originally Posted by quzah
    Wait a second. Flushing the input stream and unbuffering it are two different things. Unbuffering refers to making it non-buffered. Meaning, as soon as you press a key, it is sent immediately. (IE: Not buffered.)
    Maybe I didn't use the best terminology, but I did have "debuffered" in quotation marks, so a winnar is me. :-)

    Quote Originally Posted by quzah
    Flushing the stream simply means to clear out any pending input. That's a snap to do:
    Code:
    void fflushstdin( void )
    {
        int c;
        while( (c = fgetc( stdin )) != EOF && c != '\n' );
    }
    That is true, but the relevant FAQ entry states that that will not work if there is no input pending. What I'm trying to accomplish is create a panacea that works whenever wherever.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, if there were one that worked in all occasions, it'd already be in the relevant FAQ entry.


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

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Can you use feof() on stdin to see if there's any input pending?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    I suppose it would work if there was EOF left in the buffer but I wouldn't rely on it.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Can you use feof() on stdin to see if there's any input pending?
    No, that tells you there will never be any more input.

    > void* p = malloc(BUFSIZ);
    Way to go with the huge memory leak!
    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.

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Quote Originally Posted by Salem
    > void* p = malloc(BUFSIZ);
    Way to go with the huge memory leak!
    What memory leak?

    Quote Originally Posted by setbuf() man page
    When the first I/O operation occurs on a file, malloc(3) is called, and a buffer is obtained.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well how many free() calls are there for every Flush_Stream() call - bzzzzzt - NONE!!!!
    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.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    inline bool Flush_Stream(FILE* stream) {
        void* p = malloc(BUFSIZ);
        if (p == NULL)
            return false;
    
        setbuf(stream, p);
    
        free(p);
        return true;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Great - now setbuf() is using free'd memory - nice bun-fight when someone else gets that same block in a following malloc call.
    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.

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Huh? But the free() call is after the setbuf() call.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I take it you haven't read the manual page on setbuf() to know that it holds onto the pointer you give it, and then uses it as the buffer for future I/O operations.
    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.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, I haven't, sorry. So where would you put the free()?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't polish a turd.


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

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, 02: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, 05:15 AM