Thread: using sscanf to skip trailing whitespace

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    6

    using sscanf to skip trailing whitespace

    hello i am a beginner in c.. and i am trying to read in user input
    the expected input is directory paths
    Code:
    int main()
    {
     char line[256] = { 0 }; 
     char extra[256] = { 0 };
     char path[256] = { 0 };
    
     printf("enter dir name: ");
     fgets(line, 1024, stdin);
     sscanf(line, "%s", path, extra);
    
    /***
     * input- C:\Program Files\Microsoft  output: C:\Program
     * input- C:\My Documents                outpu:  C:\My
     */
     printf("%s\n", path);
    }
    is it possible to skip trailing whitespace with sscanf?
    thanks for the help.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Whitespace is the normal field separator for sscanf (at least on my old compiler).

    If you still have whitespace at the end, I see three ways you could remove it:

    1) start at the first char and work back in the string, char by char, until you reach the whitespace char you don't want. Snip the rest off by inserting an EOS '\0' char at that position in the string.

    2) if the dir string contains whitespace, then start at the end of the dir string, move thru the string backward until you reach the first non-whitespace char. Move back 1 char and insert the EOS char at that position in the string.

    3) Learn how your sscanf can work to allow only certain char's to be accepted. In my old compiler, I found that to be a PITA to do. You might find it easier with your newer version of C.

    Believe it or not, my old compiler has examples of fflush(stdin), right in their examples!

    Adak

  3. #3
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        char line[1024] = { 0 }; 
        char extra[256] = { 0 };
        char path[256] = { 0 };
    
        printf("enter dir name: ");
        fgets(line, 1024, stdin);
        
        strncpy( path, line, 255 );             // Copy maximum of 255 characters from line to path
        path[ strlen( path ) - 1 ] = '\0';      // Replace newline character with NULL character
                                                // I am unsure if the above line of code is very good practice, even though it works.
        
        printf("%s\n\n", path );
        
        return 0;
    }

    The above will work, though as I mentioned in the comments, I don't know what our strict brothers here will think about my newline hack.


    [EDIT]
    Holy crap, I missed one big error you made. Your call to fgets is allowing 1024 bytes to be read into a buffer (line[]) which is only 256 bytes - BAD! If the user enters more than 256 characters, you enter undefined land - you don't want to go there. :P
    Last edited by bivhitscar; 09-25-2006 at 05:37 AM.
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I don't know what our strict brothers here will think about my newline hack.
    It assumes that a newline is present. If you do a forum search, you'll probably find a thread where we tossed around lots of ideas for this. Though the generally accepted solution is:
    Code:
    char *newline = strchr ( array, '\n' );
    
    if ( newline != NULL )
      *newline = '\0';
    >If the user enters more than 256 characters, you enter undefined land - you don't want to go there. :P
    And you can avoid it by using sizeof rather than a magic number:
    Code:
    fgets ( array, sizeof array, stdin );
    Though this assumes the array is actually an array. If it's a pointer to dynamically allocated memory or the argument to a function, sizeof won't work directly.
    My best code is written with the delete key.

  5. #5
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Yes, I knew that making an assumption was going to be an issue. And the solution seems good, something I'd probably end up with if I was told to make no assumptions.

    As for the magic number, I understand the reasoning there and fair enough too. But a method Salem often uses as an example is:

    Code:
    char buf[BUFSIZ];
    
    fgets( buf, BUFSIZ, stdin );
    Is that not the same thing?
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    They both have "problems":

    The "sizeof object" method only works if the array is defined in scope, and not passed as an argument.
    The defined object method works until you later decide you don't want BUFSIZ any more, and want ... whatever ... then you have to go search and replace.

    They both work, if you use them correctly. Funny how that is.


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

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is that not the same thing?
    Well, it's still a bit of an issue because you can't redefine a standard macro, and it can turn into a big problem if you try to use a different array with a different size and forget to update the size parameter.
    My best code is written with the delete key.

  8. #8
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Ahh ok, so they are only a problem if you don't keep track of what you're doing. But in a technical sense, there really isn't a better way to do the job. I think I've got it now. :P
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching for a string in txt file with whitespace!
    By invisible_wall in forum C++ Programming
    Replies: 0
    Last Post: 03-29-2009, 10:02 PM
  2. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 06:42 AM
  3. Removing leading and trailing whitespace
    By JimpsEd in forum C Programming
    Replies: 2
    Last Post: 05-14-2006, 03:55 PM
  4. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  5. Removing Leading & Trailing Whitespace
    By EvilGuru in forum C Programming
    Replies: 11
    Last Post: 12-01-2005, 02:59 PM