Parsing a line using sscanf()

This is a discussion on Parsing a line using sscanf() within the C Programming forums, part of the General Programming Boards category; Let's say I have a line like this: 1 yellow car 550.00 How can I parse this line into int ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Parsing a line using sscanf()

    Let's say I have a line like this:
    1 yellow car 550.00

    How can I parse this line into int quantity, char description[40] and float price and detect if the user does not use that format? I tried this:
    Code:
    if(sscanf(line, "%d[^' ',%39[^' '],%f", quantity, description, price) != 3){
            printf("Error\n");
            return 0;
        }
    but it doesn't work. Thanks.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    The first thing that comes to my mind is strtok(). You specify a delimiting character, in your case, ' ' (space), and then call strtok() several times, assigning the result to different pointers. It will replace the next space with a null character, and return a pointer to the beginning of that token, so you'd end up with pointers to the following strings:

    Code:
    1\0
    yellow\0
    car\0
    550.00\0
    Then you'd just convert the first and last strings into numerical values using atoi().

    I may be missing a really obvious solution, but this should work.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Well you seem to have completely mis-understood what character classes are in sscanf.

    Try this
    Code:
    #include <stdio.h>
    
    int main ( ) {
        char test[] = "1 yellow car 550.00";
        int ncars;
        char description[100];
        float price;
        int scanresult;
        
        scanresult = sscanf( test, "%d %[^0-9] %f", &ncars, description, &price );
        if ( scanresult == 3 ) {
            printf( "N=%d, Desc=%s, Price=%f\n", ncars, description, price );
        } else {
            printf( "Bad format\n" );
        }
        
        return 0;
    }
    Basically, it relies on descriptions never having digits, so "2 Mazda MX2 2345.00" would break this code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Salem, thanks.
    Could you point me to a good place where I can read about character classes in sscanf()?

    Also, what if I want to use sscanf() just to parse the line and then validate each variable individually? Can I ignore the return value from sscanf()? How would I allow numbers to be read into the variable description? Thanks.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > Could you point me to a good place where I can read about character classes in sscanf()?
    Any decent C book

    If you're going to parse each value manually, I see no point in having an initial pass with sscanf.

    > How would I allow numbers to be read into the variable description?
    It's going to be tough using scan sets, that's for sure. Unlike PERL say, where you can have qualified look-ahead to determine whether you should be including stuff or not.
    Files with unique delimiters are much easier to parse.

    I suppose in this case, the first int is the quantity, the last float is the price, and everything else is the description.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    I changed the way the user inputs variable line. Now it's going to be like this:
    1,yellow car,550.00

    I was able to parse the line myself but I have one last problem (I hope). How do I stop special characters (ie: &^%$#@!&*) from being read into variable description. I think it has something to do with their ASCII value but I am not sure. Thanks.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    Well once you've read the description, loop over it calling isalpha() or isprint()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parsing command line strings
    By John_L in forum C Programming
    Replies: 15
    Last Post: 05-28-2008, 09:26 AM
  2. sscanf and parsing strings
    By jco1323 in forum C Programming
    Replies: 4
    Last Post: 02-20-2008, 06:32 PM
  3. Command Line Argument Parsing
    By lithium in forum Windows Programming
    Replies: 3
    Last Post: 07-13-2005, 08:01 PM
  4. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 11:22 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21