Thread: scanf and whitespaces

  1. #16
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by WaltP
    Alternatively:
    Code:
    char buf[20];
    char clear[20];
    int  len;
    
    fgets(buf, 20, stdin);    // get a line
    len = strlen(buf) - 1;    // last character's position
    if (buf[len] == '\n')     // test for RETURN
    {
        buf[len] = '\0';     // clear RETURN
    }
    else
    {        // Too many characters were entered
        do   // read off the rest of the input buffer
        {
            fgets(clear, 20, stdin);  // get excess
            len = strlen(clear) - 1;
        }  while (clear[len] != '\n');
    }
    Consider what happens when EOF is sent with no characters. len is -1, because strlen(buf) is 0, and you get an infinite loop because there's no \n and the stream is EOF.

  2. #17
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by cwr
    Consider what happens when EOF is sent with no characters. len is -1, because strlen(buf) is 0, and you get an infinite loop because there's no \n and the stream is EOF.
    Good point... Although EOF is a little odd to enter from the keybord. Yes I know it's possible, but most users don't know how...
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #18
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by WaltP
    Good point... Although EOF is a little odd to enter from the keybord. Yes I know it's possible, but most users don't know how...
    So don't forget to check for a successful call to fgets as well. And remember to consider piping.
    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.*

  4. #19
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    Code:
    vehicle_make[strlen(vehicle_make)-1]='\0' ;
    this is more than enough.


    a newline character is considered a valid character and it can end the reading but in anycase A null character is always appended at the end of the resulting string.

    if a newline is there vehicle_make[strlen(vehicle_make)-1] will contain the newline else it shall contain the default null character.
    Last edited by qqqqxxxx; 03-06-2006 at 12:49 AM.

  5. #20
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by qqqqxxxx
    Code:
    vehicle_make[strlen(vehicle_make)-1]='\0' ;
    this is more than enough.


    a newline character is considered a valid character and it can end the reading but in anycase A null character is always appended at the end of the resulting string.

    if a newline is there vehicle_make[strlen(vehicle_make)-1] will contain the newline else it shall contain the default null character.
    It's actually a little too much. If the newline isn't there, a non-null character will be there -- and you overwrite it anyway.
    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.*

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Didn't we already cover that? Since Prelude hasn't said anything, I'll steal this solution:
    Code:
    vehicle_make[ strspn( vehicle_make, "\n" ) ] = '\0';
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #22
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by quzah
    Didn't we already cover that? Since Prelude hasn't said anything, I'll steal this solution:
    Code:
    vehicle_make[ strspn( vehicle_make, "\n" ) ] = '\0';
    Quzah.
    You mean strcspn?

  8. #23
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No, I meant strspn, which is why I used "\n". But that'll work too. :P (Actually I have never had occasion to use either one, which is why I never remember their names.)


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #24
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    The strspn() function calculates the length of the initial segment of s which consists entirely of characters in accept. So strspn("foo\n", "\n"); would return 0, since f is not \n.

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well that'll teach me (not really, but it should) to post tired. I read that backwards. Yeah, strcspn is what you want.

    [edit]
    I was thinking the pair of them were along the lines of strstr and its counterpart, strchr. Where one works with a string to search for, and one just searches for a single character.
    [edit]

    Quzah.
    Last edited by quzah; 03-07-2006 at 03:51 AM.
    Hope is the first step on the road to disappointment.

  11. #26
    1ST » R. vd Kooij
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    154
    Wow, haven't been here since I thought I had a working solution though I see there's been quite some uproar about this question . I've replaced the 'strlen line' with the strcspn one as it seems to be a more watertight solution. Thanks again for all the help, good to see Quzah is also still learning

Popular pages Recent additions subscribe to a feed