Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C : flushing input stream

  1. Jan 13, 2010 #1
    hi..
    i need to ask something related to c programming.. (and i don't know if this is the right place to put up this question.. so please redirect me if necessary!)

    i've read that using fflush(stdin) in C (to flush the input stream) is not a defined way to use fflush..
    but, i continue to see it being used successfully in various instances.. please tell how and why it works..?
     
  2. jcsd
  3. Jan 13, 2010 #2
    fflush(stdin) has the effect of removing any data sitting in stdin's buffer at the moment of the call.
     
  4. Jan 13, 2010 #3
    if it does that without any problem.. then why it is not advisable to use fflush() on input streams..??
     
  5. Jan 13, 2010 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    It might have that effect -- on your computer with your compiler. What effect it will have elsewhere, who knows?

    From the C standard (emphasis mine),
    int fflush(FILE *ostream);

    ostream points to an output stream or an update stream in which the
    most recent operation was not input, the fflush function causes any
    unwritten data for that stream to be delivered to the host environment to
    be written to the file; otherwise, the behavior is undefined.


    Intentionally invoking http://en.wikipedia.org/wiki/Undefined_behavior" [Broken] is a very, very bad idea.
     
    Last edited by a moderator: May 4, 2017
  6. Jan 13, 2010 #5
    ok..

    and what happens to the data that is removed from the stream.. is it simply deleted.. or something else?
     
  7. Jan 13, 2010 #6

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Who knows? What fflush does when applied to an input stream is undefined. What that means is that the compiler developers are free to do anything they please in response. fflush(stdin) might
    • Throw away any characters that happen to be in the input buffer,
    • Do absolutely nothing,
    • Cause your program to crash, or even
    • Make your computer emit http://catb.org/jargon/html/N/nasal-demons.html" [Broken].
     
    Last edited by a moderator: May 4, 2017
  8. Jan 13, 2010 #7
    no i meant.. what will fflush() do to the data in output stream, if applied to it..
     
  9. Jan 13, 2010 #8
    and what are 'nasal demons' ?!!
     
  10. Jan 13, 2010 #9

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Different question! fflush() when applied to an output stream causes any buffered output to be sent to the output device. IO devices are very, very slow compared to memory. A program that uses unbuffered output often runs much slower than a similar program that uses buffered output. In unbuffered output, characters are immediately sent to the output device. In buffered output, the output handler stores output data and later sends the buffered data to the output device en masse. Typically this happens when the output buffer becomes full or when some special character such as a newline is sent. You as a programmer have some control over how the buffering works via the function setvbuf().
     
    Last edited: Jan 13, 2010
  11. Jan 13, 2010 #10

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Click on that term. It is a hyperlink; you can see that by the underlining and the fact that your cursor changes from a normal arrow to a hand with a finger when you move your mouse over the text.
     
  12. Jan 13, 2010 #11
    yeah.. sorry.. didn't see the hyperlink!!
    and thnx for all that help..
     
  13. Jan 13, 2010 #12
    Strictly speaking, that's up to standard library developers rather than compiler developers. MSVC standard library and any others compatible with it (such as Intel library for Windows) do what I said: wiping all pending data from the input buffer. GCC standard library (glibc) does nothing and returns an error.

    As long as we're nitpicking, I might as well point out that stdin is not necessarily a read-only stream.
     
  14. Jan 13, 2010 #13
    [As long as we're nitpicking, I might as well point out that stdin is not necessarily a read-only stream.[/QUOTE]

    can u please shed some light on what a stream is in general... and what are different types of 'em..?
     
  15. Jan 13, 2010 #14

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Strictly speaking, yes. Practically speaking, no. C compilers almost always come bundled with a library.

    That's because the response to fflush(stdin) is undefined. What that means is that implementors of fflush are free to do whatever they wish when passed an input stream (or an update stream on which the last operation was input) and remain compliant with the standard. Programmers are strongly recommended to avoid undefined behavior.

    O rly?

    stdin is the standard input stream. Using it as an output stream is once again undefined.
     
  16. Jan 13, 2010 #15
    It is initially opened as an input stream. There's nothing to keep you from calling freopen("filename", "wb", stdin), or, on many platforms, even doing "stdin = stdout; "!
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook