Thread: fflush(stdin)

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > that's not a reliable way to do it either -- what happens when the string contains multiple occurrences of '\n' ?
    fgets() can only return at most ONE \n
    If fgets() does store a \n, then it will always be at the end of the buffer, just before the \0

  2. #17
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    Quote Originally Posted by Salem
    > 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.
    i enter text, how can buffer be a pointer?..

    As for the whole "it doing the same thing" issue, yuo are of course correct. I actually copied similar code from the faq and there it had a different if, but as that didin't work, I changed the if part without realising that that was what the else did. now I'm just using

    buffer[strlen(buffer)-1]='\0';

    and it works for just "/n" and for "blabla/n"...

  3. #18
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by mako
    i enter text, how can buffer be a pointer?..
    Code:
    void input_text( char * buffer ) {
       fgets(buffer, 20, stdin);
       int sz = sizeof(buffer);  // now buffer is a pointer 
                                 // check yourself what sizeof returns. hint: it's not 20
    }
    
    int main() {
       char buffer[20]; // this is an array ( sizeof(buffer) will be 20 );
       input_text(  buffer );
    }
    Kurt

  4. #19
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by mako
    As for MS-compiler, I'm using mingw which implements gcc. gcc is not ms. Does that mean mingw have added this functionality?..
    Quote Originally Posted by ZuK
    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.
    It does. MinGW executables use msvcrt.dll. (Not sure if this is changable or not, but mine does.) fflush() is located within msvcrt.dll, I believe.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #20
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    Quote Originally Posted by ZuK
    Code:
    void input_text( char * buffer ) {
       fgets(buffer, 20, stdin);
       int sz = sizeof(buffer);  // now buffer is a pointer 
                                 // check yourself what sizeof returns. hint: it's not 20
    }
    
    int main() {
       char buffer[20]; // this is an array ( sizeof(buffer) will be 20 );
       input_text(  buffer );
    }
    Kurt

    my point is that the application inputs text from a user. I've tried both cases, /n and text/n and it works for both. There's no way a user can input a pointer, so I don't see any need to adapt my program. It suits my needs just fine.

  6. #21
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Salem's point was not that any user might input a pointer. The point was if you ( at some time ) put your code into a function ( as in my example ) the array degenerates to a pointer and then the code
    Code:
    if(strlen(buffer)==(sizeof(buffer)-1)) buffer[strlen(buffer)-1]='\0';
    breaks

    BTW there is another aspect of you code that is wrong. In the case that the user inputs more text then n-1 characters there will not be a '\n' at the end of the string that fgets() returns and you will unnecessarily overwrite the last character.

    Kurt
    Last edited by ZuK; 01-21-2006 at 12:05 PM.

  7. #22
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well at least ZuK understands the issues

  8. #23
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by Salem
    > that's not a reliable way to do it either -- what happens when the string contains multiple occurrences of '\n' ?
    fgets() can only return at most ONE \n
    If fgets() does store a \n, then it will always be at the end of the buffer, just before the \0
    Yep you are right -- I even tried it in XP with a *nix file and it still worked, and even when the file was opened in binary mode.

  9. #24
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    fgets() reads characters until the size of the buffer is reached, a '\n', or EOF or some other error (as you have discovered ).

    Code:
    char buf[80];
    int len;
    fgets(buf,sizeof(buf),stdin);
    len = strlen(buf)-1;
    if( buf[len] == '\n')
       buf[len] = 0;
    ->
    Code:
    char buf[80];
    size_t len;
    fgets(buf,sizeof(buf),stdin);
    len = strlen(buf)-1;
    if( buf[len] == '\n')
       buf[len] = 0;
    strlen() returns a size_t, not an int. Your compiler shold catch this if you enable warnings.

    Dave_Sinkula, in reference to your link, does fflush(stdout) clear errors from a stream? Because if it doesn't, you'll need a clearerr() in there too.

    Salem's point was not that any user might input a pointer. The point was if you ( at some time ) put your code into a function ( as in my example ) the array degenerates to a pointer and then the code
    Code:
    if(strlen(buffer)==(sizeof(buffer)-1)) buffer[strlen(buffer)-1]='\0';
    breaks
    That's because sizeof returns the size of the variable in memory. For a char array, it returns the number of characters in the array, because that's how much memory the array takes up. But for a pointer, it will return 4 (on 32 bit machines) or 2 (on 16 bit machines) or maybe 8 (for 64 bit ones).

    Another thing wrong with that code is that calling strlen twice invokes a lot of unnessesary overhead. You can save the value (remember to use a size_t!), as AD did.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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