Thread: Sscanf Issues and getting rid of quotes

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    34

    Sscanf Issues and getting rid of quotes

    I am trying to read from a file , that contains.

    "15668942" "TOYOTA" "ENGINE PISTON" 12.50 8
    "68465245" "NISSAN" "STARTER" 50.20 14
    "14134455" "MITSUBISHI" "STEERING WHEEL" 20.00 0
    "31457645" "SUBARU" "TURBO" 3000.00 5
    $$$$$$$$$$

    I tried using sscanf. When the files reaches $$$$$ it should stop reading from the file. I havent able to get the $$$ to work as yet so i used NULL.

    My problem is 2 things.

    1) When I use sscanf and i Print it, it got the number correct, and the word toyota correct. When it came to engine piston, it took up to "engine " and used the rest for the double variable it seems to me because the output for the double and integer are crazy numbers.


    Code:
    while(fgets(line,100,in)!=NULL)
    {
    sscanf(line, "%s %s %s %e %d",number,name,description,&unit_price,&quantity_in_stock);
                printf("%s %s %s %e %d\n",number,name,description,unit_price,quantity_in_stock);
    }
    2) When I do get the values and strings in their variables, I also get the quotes. I would like to get rid of those quotes. I tried using strtok but I am getting trouble for it to take off the " because of the syntax. For example

    Code:
    resule=strtok(name,""");
    I know this does not work lol.

    Any advice.
    Last edited by Ramkiller1; 10-27-2012 at 08:03 PM. Reason: Yes, I know I should be using sizeof() instead of 100

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    sscanf's %s specifier will stop when it encounters a whitespace. Which means it will stop in the middle of "ENGINE PISTON" (just before the P). It will then fail to read subsequent floating point or integral variables, because the 'P' is still there to be read. It is usually a good idea to check scanf's return value as well, to detect errors.

    A double quote character in a string literal or in a character literal needs to be escaped with a backslash.

    Code:
    resule=strtok(name,"\"");
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Thanks for the information. Should I use fscanf then?

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    Actually,
    Code:
     
    resule=strtok(name,"\"");
    only got rid of the last " in every string. Like "15668942", turned into "15668942

    It seems fscanf has the same issue also.
    Last edited by Ramkiller1; 10-27-2012 at 08:37 PM.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    34
    I made a great deal of changes. All I am trying to do now is get 12.50 8 in their variables.

    I tried using
    Code:
    fseek(in,i,SEEK_SET);
        fgets(l,sizeof(l),in);
        sscanf(l,"%f %d",&unit_price,&quantity_in_stock);
    unit_price is declared as a double variable.

    I get the quantity_in_stock to be 8 but the unit_price is giving me crazy numbers. Any Advice?

    Quote Originally Posted by grumpy View Post
    sscanf's %s specifier will stop when it encounters a whitespace. Which means it will stop in the middle of "ENGINE PISTON" (just before the P). It will then fail to read subsequent floating point or integral variables, because the 'P' is still there to be read. It is usually a good idea to check scanf's return value as well, to detect errors.

    A double quote character in a string literal or in a character literal needs to be escaped with a backslash.

    Code:
    resule=strtok(name,"\"");

  6. #6
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    If you have input lines of format
    Code:
    "15668942" "TOYOTA" "ENGINE PISTON" 12.50 8
    "68465245" "NISSAN" "STARTER" 50.20 14
    "14134455" "MITSUBISHI" "STEERING WHEEL" 20.00 0
    "31457645" "SUBARU" "TURBO" 3000.00 5
    then why don't you use something like the following?
    Code:
    unsigned long  number;
    char           model[32];
    char           part[24];
    double         price;
    unsigned int   count;
    
    if (sscanf(line, "\"%lu\" \"%31[^\"]\" \"%23[^\"]\" %lf %u", &number, model, part, &price, &count) >= 5)
        /* You have a valid line, and parsed it into the five variables */
    else
        /* The input line does not match the above pattern */
    The [ format specifier is like s, except you list either the characters you want, or a caret ^ and the characters you do not accept. The function obviously returns the number of conversions it did, so if it returns less than five, the line did not match the given pattern.

    For strings, remember to specify the maximum length that can be stored in the char array. Also remember that that count does not include the terminating '\0', so the number for sscanf() is at least one less than you have reserved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf() issues
    By Stiletto in forum C Programming
    Replies: 11
    Last Post: 12-19-2011, 02:59 PM
  2. Computer Quotes
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-01-2003, 08:31 AM
  3. Quotes in Quotes?
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 01-12-2003, 04:48 PM
  4. Quotes
    By whistlenm1 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 07-31-2002, 09:28 AM
  5. outputing quotes(') and double quotes(")
    By lostboy in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 02-26-2002, 06:17 PM