fflush not flusing stdin!

Discussion in 'C Programming' started by jaswinder, Aug 18, 2010.

  1. jaswinder

    jaswinder Guest

    Greetings:

    While working on a computer science assignment I have had the need to
    flush the contents of stdin. But the statement

    fflush(stdin);

    doesn't seem to be working. On the other hand:

    char s[80];

    gets(s);

    does seem to be working provided that whatever is in the stdin is not
    longer than 80 characters.

    Could anyone kindly point out why fflush failed in this situation.
     
    jaswinder, Aug 18, 2010
    #1
    1. Advertising

  2. jaswinder

    Ian Collins Guest

    On 08/19/10 09:56 AM, jaswinder wrote:
    > Greetings:
    >
    > While working on a computer science assignment I have had the need to
    > flush the contents of stdin. But the statement
    >
    > fflush(stdin);
    >
    > doesn't seem to be working. On the other hand:
    >
    > char s[80];
    >
    > gets(s);
    >
    > does seem to be working provided that whatever is in the stdin is not
    > longer than 80 characters.
    >
    > Could anyone kindly point out why fflush failed in this situation.


    C99 7.19.5.2 The fflush function

    2 If stream 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.

    In other words, you can't use fflush on stdin.

    --
    Ian Collins
     
    Ian Collins, Aug 18, 2010
    #2
    1. Advertising

  3. jaswinder

    Seebs Guest

    On 2010-08-18, Ian Collins <> wrote:
    > On 08/19/10 09:56 AM, jaswinder wrote:
    >> While working on a computer science assignment I have had the need to
    >> flush the contents of stdin. But the statement


    >> fflush(stdin);


    >> doesn't seem to be working.


    > In other words, you can't use fflush on stdin.


    If this seems weird, here's a way to think about why that limitation makes
    sense:

    How much data have you written to stdin recently?

    If you haven't written anything to stdin, what makes you think there is
    any data that you've written which remains unflushed?

    To "flush" a buffer is not to "discard", it's to "ensure completion of
    writing". If you want to *discard* data, you need something else. (The
    Berkeley libc has fpurge() for exactly that, other systems may overload
    fflush() to do it, despite the misleading semantics, or not provide a means,
    or...)

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Aug 18, 2010
    #3
  4. On 18 Aug, 22:56, jaswinder <> wrote:

    > While working on a computer science assignment I have had the need to
    > flush the contents of stdin.


    no you didn't!


    > But the statement
    >
    > fflush(stdin);
    >
    > doesn't seem to be working.


    [line moved]
    > Could anyone kindly point out why fflush failed in this situation.


    as others have pointed out fflush() only works for (in a sense only
    has a meaningful defintion for) output streams. I'm not sure exactly
    what you expect but one possibility is you want to skip to the end of
    line.

    int skip_line (FILE *instream)
    {
    int ch;
    while ((ch = gets(instream)) != '\n' && ch != EOF)
    ;
    return ch;
    }

    functions like this are tricker to write than you might expect, so
    expect a few brickbats to come my way!

    > On the other hand:
    >
    > char s[80];
    >
    > gets(s);


    oooH! NEVER DO THAT

    gets() is borken. It has no way to indicate how big its buffer is.
    (fgets() can be used instead, though it doesn't do quite the same
    thing as gets(), read the documentation).

    > [gets()] does seem to be working provided that whatever is in
    > the stdin is not longer than 80 characters.


    and if it is... where do those extra characters go? On most systems
    all over your other data. This is the way in for an amazing number of
    virii and other malware.

    http://en.wikipedia.org/wiki/Conficker
    (ok, I don't know it was gets(), but it *was* a buffer overflow!)

    You may want to read this
    http://c-faq.com/

    Particularly:-
    12.26a How can I flush pending input so that a user's typeahead isn't
    read at the next prompt? Will fflush(stdin) work?

    12.26b If fflush won't work, what can I use to flush input?

    12.23 Why does everyone say not to use gets()?

    In fact read all of section 12. Then read all of the FAQ. Some you
    might not understand or haven't studied yet and some may seem over
    picky. Re-read when you learn a little more C and you'll appreciate it
    more.



    --

    A ruby trembled. Two tourmaline nets failed to rectify the laser beam.
    A diamond noted the error. Both the error and the correction went into
    the general computer.
    Corwainer Smith "The Dead Lady of Clown Town"
     
    Nick Keighley, Aug 19, 2010
    #4
  5. Nick Keighley <> wrote:
    > int skip_line (FILE *instream)
    > {
    > int ch;
    > while ((ch = gets(instream)) != '\n' && ch != EOF)

    ^^^^

    I am 100% sure you meant getc() instead of gets() here;-)

    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Aug 19, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    15,730
    Ali Cehreli
    Aug 10, 2004
  2. hugo27
    Replies:
    5
    Views:
    655
    Chris Torek
    Oct 10, 2004
  3. asit

    fflush and stdin

    asit, Jan 14, 2008, in forum: C Programming
    Replies:
    17
    Views:
    869
    Randy Howard
    Jan 20, 2008
  4. Tagore

    fflush(stdin), scanf and a space

    Tagore, Dec 28, 2008, in forum: C Programming
    Replies:
    5
    Views:
    4,935
    Eric Sosman
    Dec 29, 2008
  5. barry

    fflush(stdin), scanf and a space

    barry, Jun 7, 2010, in forum: C Programming
    Replies:
    5
    Views:
    6,210
    Eric Sosman
    Jun 9, 2010
Loading...

Share This Page