#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Fget clean buffer problematic


    char line[120];
    fgets(line,120,stdin);



    when i put above 120 char, it's making problem. how do i clean extra chars (above 120)?
    fflush(stdin) won't help in linux.

    thanks for the good helpers!!
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    In C++, this would be trivial:
    Code:
    std::string line;
    getline(std::cin, line);
    In C, it is a bit more convoluted. See http://www.stroustrup.com/new_learning.pdf for a discussion of how to handle this in C.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    hmm


    didnt find info on that... i'm using C - any idea??
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Did you even read the PDF that I linked above. The author presents a complete working solution in C in the first 3 pages.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    Code:
    char line[120];
    
    // Mark the end of the buffer - allowing for the nul added by fgets()
    line[sizeof(line) - 2] = '\n';
    fgets( line, sizeof(line), stdin ) ;
    
    // If the end character was changed and is not a newline, then
    // buffered characters remain to be flushed...
    if( line[sizeof(line) - 2] != '\n' )
    {
        // get all buffered characters until the newline is found...
        while( getchar() != '\n' )
        {
            // do nothing
        }
    }

    Comments on this post

    • Lux Perpetua agrees

IMN logo majestic logo threadwatch logo seochat tools logo