Thread: Flush Input Buffer

  1. #31
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by mabuhay
    what means "fflush() is illegal"? you shouldn't use it anymore?
    I normally use fflush(stdin) after fgets()...
    fflush() is ok, when it is used in the proper manner. Read the FAQ on it.

  2. #32
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by kermit
    fflush() is ok
    My compiler, VC++ 6.0, doesn't like it. fflush() requires one parameter.

  3. #33
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >My compiler, VC++ 6.0, doesn't like it. fflush() requires one parameter.
    This is precisely why I don't add parens to function names when I'm talking about them. To clarify for everyone who doesn't like anything titled "FAQ", fflush ( stdout ) is good, fflush ( stdin ) is bad. fflush is defined as only working on output streams, and passing it an input stream is undefined according to the standard.
    My best code is written with the delete key.

  4. #34
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    I'm just too lazy to write the parameters...

    but, does someone had ever problems using fflush(stdin)?
    i think it is known, if you use fgets(some_parameters*g*), that you have to empty the stdin if you want to use fgets again. So why is there no c-function to handle this?
    Last edited by mabuhay; 10-28-2005 at 04:22 PM.

  5. #35
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >does someone had ever problems using fflush(stdin)?
    Go to a non-Windows system and try to use it.

    >if you use fgets(some_parameters*g*), that you have to empty the stdin if you want to use fgets again
    Only if the line is longer than your buffer, otherwise fgets has no problems. You're probably thinking of using scanf, then the a newline probably has to be extracted before fgets will do what you want.

    >So why is there no c-function to handle this?
    Because if you did things the right way to begin with, you wouldn't have problems.
    My best code is written with the delete key.

  6. #36
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by mabuhay
    I'm just too lazy to write the parameters...
    but, does someone had ever problems using fflush(stdin)?
    Even if fflush(stdin) were somehow "valid", it doesn't even make logical sense. When you flush an output buffer, all data that is buffered will be written out. The logical opposite of this when applied to input buffers is that all data that is buffered will be read in. Guess what does this? Functions that read from the stream (fgets/scanf/fread/getc/etc.).

    You don't want to discard input, you want to process it and deal with it.

  7. #37
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by Ancient Dragon
    My compiler, VC++ 6.0, doesn't like it. fflush() requires one parameter.
    ah - haha

    I tend to write my functions that way (with the parentheses) just to indicate I am referring to a function. I never considered that it would be confusing that way though.

  8. #38
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    Quote Originally Posted by cwr
    ...You don't want to discard input, you want to process it and deal with it.
    Yes, but if I use fgets() I stored my input in a variable but something is still in the input. Or why does it jups over if I use fgets() in the same code again?

    Quote Originally Posted by Prelude
    ...You're probably thinking of using scanf,...
    I don't want to use scanf(), because you can't define the input-length globally.

    Ok, can someone give me a code example how to use fgets() correctly? If i want to take three lines of text one after other, each with maximal 50 characters.

  9. #39
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by mabuhay
    Yes, but if I use fgets() I stored my input in a variable but something is still in the input. Or why does it jups over if I use fgets() in the same code again?
    Really?


    I don't want to use scanf(), because you can't define the input-length globally.
    You can, it's just not particularly convenient.

    Ok, can someone give me a code example how to use fgets() correctly? If i want to take three lines of text one after other, each with maximal 50 characters.
    Well, I can illustrate an issue with fgets:
    Code:
    #include <stdio.h>
    
    #define NLINES 3
    
    int main(void)
    {
        char buf[NLINES][50];
        int i;
        for (i = 0; i < NLINES; i++)
        {
            printf("Enter line %d: ", i);
            fflush(stdout);
            fgets(buf[i], sizeof buf[i], stdin);
        }
        for (i = 0; i < NLINES; i++)
            printf("Line %d was \"%s\"\n", i, buf[i]);
        return 0;
    }
    Output:
    Code:
    Enter line 0: Line number zero
    Enter line 1: A really long line one that exceeds fifty characters
    Enter line 2: Line 0 was "Line number zero
    "
    Line 1 was "A really long line one that exceeds fifty characte"
    Line 2 was "rs
    "
    The problem being that fgets will stop reading if it hits the size limit. One way to check to see if fgets has read a whole line is to check for the presence of a \n on the end, and discard further input with another fgets (which may overflow again), or a getchar loop, until you reach the actual end of line.

  10. #40
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by cwr
    One way to check to see if fgets has read a whole line is to check for the presence of a \n on the end
    That may not always work either if the input stream does not contain a '\n'. For example the last line of a file may not terminate with '\n', in that case fgets() will not put one at the end of the line either.

  11. #41
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    True, so you should also check for a NULL return or an feof on the stream.

  12. #42
    Registered User
    Join Date
    Oct 2005
    Posts
    26
    OK, now i got it! If ther are too much characters in the input buffer, the maximal number of characters will be read and the rest will stay in the stdin and if the next scanf() is called, it will take the remaining characters. Because the end of the remaining characters is a "\n" it will straight continue.

    now i understand why cwr said:
    ...You don't want to discard input, you want to process it and deal with it.
    i thought, the input that is already processed (copyed in your variable) is still in the stdin.

    and Prelude said:
    ...You're probably thinking of using scanf...
    when you use scanf to read just one character (scanf("%c", &character); ) then the next scanf() will be jumped over and it seems that in the variable of the second scanf() is nothing, but there's the "\n" in it.

    thx for your help and patience
    Last edited by mabuhay; 10-29-2005 at 09:02 AM.

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