Thread: fflush(stdin)

  1. #1
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69

    fflush(stdin)

    okay. I know this function is wrong, but I have an example where the code doesn't exectute as it should when I leave it out. I'm afraid the code is commented in german, sa are the variables.

    Basically what it does is read in 3 sets of two strings and a number into a struct, and then write the values to a file.

    Now after reading the values to the struct there is an fflush(stdin). If I leave it out, on the second(/3) time writing to the struct, it only asks fo rthe second string.

    I'm using gets to read the strings into the struct and fprintf to write to the file. Any ideas?..

    I won't add all the code cos I'm pretty sure the problem lies in the following segment, however if somebody want's to see the problem themselves, just say and I'll put up the whole program. It's relatively short anyway...

    Code:
    int readin()
      {
         printf("Name: ");       
         gets(aktuell->name);
         printf("Vorname: ");
         gets(aktuell->vorname);
         printf("PLZ: ");
         scanf("%d",&aktuell->PLZ);
    
         fflush(stdin);
    
    fprintf(fp, "%20s %20s %5d", aktuell->name, aktuell->vorname, aktuell->PLZ);
    WITH the fflush(stdin) the Program reads:

    Code:
    Name: (I enter xxx)
    Vorname: xxx
    PLZ: (I enter yyy cos it's a number)
    
    Name: xxx
    Vorname: xxx
    PLZ: yyy
    
    Name: xxx
    Vorname: xxx
    PLZ: yyy
    AND WITHOUT:

    Code:
    Name: xxx
    Vorname: xxx
    PLZ: yyy
    
    Name: Vorname: xxx
    PLZ: yyy
    
    Name: Vorname: xxx
    PLZ: yyy
    the output on the first is right, the second returns nothing on the last two Name's...

    Many thanks in advance,..

    ~mako

    €DIT: Please don't link me to the FAQ, I've read th epart on fflush(stdin) and it doesn't answer why it works here and doesn't without...

    And I don't think it's compiler related here either. If you don't believe me, ask for the full program and try executing yourself... Thx again...
    Last edited by mako; 01-20-2006 at 10:38 AM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The point of fflush(stdin) is to flush the input buffer. The problem is that the C standard states that fflush(stdin) is undefined. Just because it is working for you here does not make it OK since it wont work on most compilers.

    Read this FAQ entry to see how to flush the input buffer correctly.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by mako
    And I don't think it's compiler related here either. If you don't believe me, ask for the full program and try executing yourself... Thx again...
    It is. Just MS-compilers support that crap.
    my output:
    Code:
    Name: xxx
    Vorname: xxx
    PLZ: 123
    Name: Vorname: xxx
    PLZ: 123
    Name: Vorname: xxx
    PLZ: 123
    Kurt

  4. #4
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    Quote Originally Posted by ZuK
    It is. Just MS-compilers support that crap.
    my output:
    Code:
    Name: xxx
    Vorname: xxx
    PLZ: 123
    Name: Vorname: xxx
    PLZ: 123
    Name: Vorname: xxx
    PLZ: 123
    Kurt

    I assume you get that output while still using the fflush(stdin); As for MS-compiler, I'm using mingw which implements gcc. gcc is not ms. Does that mean mingw have added this functionality?..

    Thanks bithub for that link, I'll make sure to look into it...

    Thanks to both of you on this quick and helpful feedback.

    ~mako

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Right fflush(stdin); is in my code.
    > I'm using mingw which implements gcc.
    Funny I use GCC 3.3.5. Maybe the really changed the library to make it compatible with the MS library. Actually I remember that I read somwhere that some of this gcc-port packages use the MS-runtimelibrary. Maybe yours does.
    Kurt

  6. #6
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    And please, don't use gets(). fgets() is so much better.

  7. #7
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    If I scanf("%d", int_var_name);, it only continues when I actually enter something. It doesn't if I just hit enter... How can I overcome this?..

    as for fgets being better than gets, apart from the fact that it requires more code, it adds an '\n' onto every string entered which is both unwanted and hard to get rid of. Why do you claim fgets to be better again? and how would you og about getting rid of that '\n'?
    Last edited by mako; 01-20-2006 at 12:59 PM.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by mako
    If I scanf("%d", int_var_name);, it only continues when I actually enter something. It doesn't if I just hit enter... How can I overcome this?..
    http://www.daniweb.com/code/snippet266.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    Quote Originally Posted by mako
    as for fgets being better than gets, apart from the fact that it requires more code, it adds an only '\n' onto every string entered which is both unwanted and hard to get rid of. Why do you claim fgets to be better again? and how would you og about getting rid of that '\n'?
    gets() does not check for the size of your buffer, so if the user writes too much text, you'll have a buffer overflow. You don't have a chance to avoid that when using gets(). The manpage says:
    Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.
    Removing the '\n' should be trivial. Just check how long your string is. If it is sizeof(buffer)-1 check if the last character before the '\0' is the '\n'. If so, overwrite it. If your string is shorter, set the character at strlen(buffer) to '\0'.

  10. #10
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    Quote Originally Posted by Dave_Sinkula

    thx, just what I needed

    as for the '\n' issue, I'm working on it, but I think I know what I need to do

  11. #11
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469

    using fgets

    there is an item in the faq regarding fgets and removing the new line, here's the link:

    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    this method is very effective, but it requires another variable and string.h so its up to you which method you prefer, but you can't go wrong with this

    as for using fflush(stdin) in the first place, i really don't see the problem with it if the program is written in a personal context. just because its not the best code, doesn't mean its not good enough for light applications. if you want to be very precise and correct, then thats another matter but if the program is for personal use, then there should be no significant problems.

    with that said, there's nothing wrong with being as professional as possible. freuen sich auf!

  12. #12
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >i really don't see the problem with it if the program is written in a personal context
    As long as it actually works (ie. documented on your compiler) and nobody sees you use it, there isn't a problem.

  13. #13
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    Code:
    fgets(buffer, 20, stdin);
    if(strlen(buffer)==(sizeof(buffer)-1)) buffer[strlen(buffer)-1]='\0';
    else buffer[strlen(buffer)-1]='\0';
    strcpy(aktuell->name, buffer);
    seems to work. o.O anybody spot any flaws?..

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > anybody spot any flaws?..
    That your if and else parts both do exactly the same thing, thus rendering the comparison useless, and the code wrong.
    That the whole method breaks down horribly (without warning) if buffer is a pointer, and not an array.
    That a more reliable way has already been posted in the FAQ.

  15. #15
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    quote from link above
    Code:
       if ((p = strchr(buf, '\n')) != NULL)
          *p = '\0';
    that's not a reliable way to do it either -- what happens when the string contains multiple occurrences of '\n' ?

    Here is how I do it. The test for '\n' at the end of the line is necessary because fgets() may not put one there when reading from a file (for example if the last line of the file does not end with '\n')
    Code:
    char buf[80];
    int len;
    fgets(buf,sizeof(buf),stdin);
    len = strlen(buf)-1;
    if( buf[len] == '\n')
       buf[len] = 0;

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