Thread: fflush(stdin)

  1. #1
    Join Date
    Feb 2002


    Before someone points me to the FAQ just for posting a thread with that kind of a subject, let me say that I already read it. That's why I have a question.

    The FAQ says it's bad. CPPReference says it's okay. says it's bad. says it's okay.

    It seems like every other source tells a different story. Which is it? And why is there so much conflicting information?
    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

  2. #2
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    just don't use it in C programming, it's undefined behaviour

  3. #3

    Some people believe that fflush(stdin); may be used to clear the input stream, stdin. Like seen here.

    Though, in reality, it is undefined behavior because the standard only provides words to make sense out of fflush() with output streams, not input streams. The reason this is so is because the stdio buffer and the operating system buffer are usually two different buffers.

    Here is a brief explanation of fflush(): If the given stream has been opened for writing operations the output buffer is phisically written to the file. When a file is closed all the buffers associated with it are automatically flushed.

    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    The problem comes because on a windows system the operating system will allow you to flush an input stream.
    But lets think about this logically for a minute:
    flushing an output stream is fairly simple, you have a buffer to store the information to write and by flushing you cause all the information to go to the assocated stream. It really doesn't matter how big or small the buffer is.
    Now lets think about an input stream. It too is buffered. So when you flush it, it should empty the buffer, but where should the data go? With the output it has an assocated file to send the information to, not so with the input stream. The size of the buffer matters in this case, what if the buffer was large enough to handle your entire file? By flushing it you've now lost all that information.

    Theres no standard method for flushing an input stream because there is no one method that will work in all cases.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > It seems like every other source tells a different story. Which is it?
    The only source worth a damn is the standard itself.

    Those sites which say it's OK have a rather unpleasant M$ after-taste.
    You can tell that the examples in the site were written by a VC++ programmer who didn't know how to compile C.
    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.

  6. #6
    Registered User
    Join Date
    Jan 2005
    in TCPL Appendix A, Kernighan and Ritchie said:
    int fflush(FILE *stream) 
    On an output stream, fflush causes any buffered but unwritten 
    data to be written; on an input stream, the effect is undefined. 
    It returns EOF for a write error, and zero otherwise.
     fflush(NULL) flushes all output streams.

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, 12:13 PM
  5. Linked List Need Help Urgent
    By ykchua in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:57 PM