sscanf and string handling question

This is a discussion on sscanf and string handling question within the C Programming forums, part of the General Programming Boards category; I need help using sscanf. I have googled it and have been testing this for a few days and i ...

  1. #1
    Registered User ursula's Avatar
    Join Date
    May 2009
    Location
    Madrid, Spain
    Posts
    11

    sscanf and string handling question

    I need help using sscanf. I have googled it and have been testing this for a few days and i just cant get it right.

    I have a string, lets say:
    Code:
    char *line="term1=the cat&term2=M"
    I need to read "the cat" and "M" and put them in two separate strings.

    I figured at first that i could divide the string in two with:
    Code:
    sscanf(line, "%[^&],%[^&]", string1, string2);
    and get "term1=the cat" on string 1 and "term2=M" on string2 and use sscanf on those in turn.

    I use this sscanf line with strings with a "," instead of a "&" and no "=" and it separates the words perfectly regardless of blank spaces, but i cant get it to separate this string since the space confuses it. (and me too)

    Any ideas?

    thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The space confuses nothing. The comma confuses sscanf, since you claim the line has a comma in it which it manifestly does not.

  3. #3
    Registered User ursula's Avatar
    Join Date
    May 2009
    Location
    Madrid, Spain
    Posts
    11
    the line has no comma its just "term1=something something&term2=somethingelse"

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ursula View Post
    the line has no comma
    Yuh-huh!
    Code:
    sscanf(line, "%[^&],%[^&]", string1, string2);
    See?


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

  5. #5
    Registered User ursula's Avatar
    Join Date
    May 2009
    Location
    Madrid, Spain
    Posts
    11
    i follow u, sorry. I thought i needed that.

    However, it still doesnt work. it extracts the first half "term1=the cat" but it ignores string2.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should post a small example. You do know that the ^ means "everything except...", which means that if this is your input:

    "term1=something something&term2=somethingelse"

    Then this is what's going to be stored in the first variable:

    "term1=something something"

    Because you don't actually have two & in your input string, it won't store up to the next one in the second variable.

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

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    post the updated sscanf()

  8. #8
    Registered User ursula's Avatar
    Join Date
    May 2009
    Location
    Madrid, Spain
    Posts
    11
    I did know about the^ but i wasnt sure how to work with that... Heres how i solved it

    Code:
    char *line="term1=something something&term2=something2";
    char *string1, *string2, *x);
    
    sscanf(line, "%[^&]&%[^&]", string1, string2); //string1="term1=something something"
                                                                       // string2="term2=something2"
    
    sscanf(string1, "%[^=]=%[^=]",x, string1);  //string1="something something"
    
    sscanf(string2, "term2=%s", string2);  //string2= "something2"

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why do you have the second %[^&] in there if you only have one & in your string? That doesn't really make sense.


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

  10. #10
    Registered User ursula's Avatar
    Join Date
    May 2009
    Location
    Madrid, Spain
    Posts
    11
    Well, ive been handling several strings with multiple &s in them. It is working like that... i guess i could also do something like
    Code:
     sscanf(line, "%[^&]&%s", string1, string2);

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here's one that works for this:
    Code:
    char *line="term1=the cat&term2=M";
    sscanf(line, "%*[a-z0-9]=%[a-z ]&%*[a-z0-9]=%[A-Z ]",&one,&two);
    I did test it. The * flag means ignore this match. The %[a-z] style stuff is to catch only the specified characters, notice there are spaces in there (you probably want to use %[a-zA-Z ] in the generic edition).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User ursula's Avatar
    Join Date
    May 2009
    Location
    Madrid, Spain
    Posts
    11
    that worked too, thanks!

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ursula View Post
    that worked too, thanks!
    All right, you're welcome. As long as you understand you are supposed to understand how that works, and not just blindly implement it . I prefer "reverse engineering" my comprehension from working examples, myself, so I thought that might be useful.

    Your other idea for parsing this in sections is not a bad one. This looks like http form data; if so you probably can do it in one line for a few fields. But if the number of fields, etc, is many and varied it will be better to break in into a hierarchy of segments. Look into strtok().
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Code:
    #include <stdio.h>
    
    main()
    {
        char *line = "term1= the cat &term2= M ";
        char one[100], two[100];
        
        if (sscanf(
                line,
                "%*[^=]="
                "%99[^&]&"
                "%*[^=]="
                "%99[^\n]",
                one,
                two
            ) != 2)
            return 1;
        printf(".%s.\n.%s.\n", one, two);
        
        return 0;
    }
    here sscanf has limit and more uniform for characters, I suggest to separate line before the processing by & and then process every part with one algorithm (because it has two equal parts "%*[^=]=" and then, when it separated, it can be "%*[^=]=%99[^\n]" format string for every part of separated source line)
    then you can put it in the function and so on ...

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yeah, that one works too, and is more generic I think. A nice contrast of two different methods (inclusive and ^exclusive) with my post.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf() string version question
    By mattflick in forum C Programming
    Replies: 3
    Last Post: 10-19-2005, 10:57 AM
  2. Question about parsing a string from a line
    By edd1986 in forum C Programming
    Replies: 2
    Last Post: 04-23-2005, 03:18 PM
  3. sscanf question
    By Little_Dump in forum C Programming
    Replies: 5
    Last Post: 10-27-2003, 01:16 PM
  4. Dumb Question: What is sscanf?
    By KingZoolerius66 in forum C Programming
    Replies: 3
    Last Post: 10-04-2003, 08:19 PM
  5. sscanf string parsing
    By penney in forum C Programming
    Replies: 5
    Last Post: 02-03-2003, 10:28 AM

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