Thread: sscanf question

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    27

    sscanf question

    Salem used sscanf in a snippet of code he gave me a bit ago, and it was interesting (I've always just done strstr parsing), but I need a bit more information about sscanf and I can't find it elsewhere x.X

    The question I have is in sscanf, when using something like %[^:], it copies everything in front of the :. How would I get it to copy everything behind it? I tried removing the ^, but it just returned nothing :/

    Thats all for now, Gotta sleep heh.. Thanks again =)

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    %[^:]:%[^:]

    is one way.

    gg

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That depends if they want to use what's in front of it or not. If not:
    Code:
    sscanf( thisstring, "%*[^:]:otherscancodeshere", ... );
    This will skip past the : and then you can add your other stuff. So will this:
    Code:
    char *p = strchr( thisstring, ':' );
    ...now call sscanf on p, assuming it's not null...
    [edit]
    Quote Originally Posted by Codeplug
    %[^:]:%[^:]

    is one way.

    gg
    And on an aside, no, it doesn't do that at all. It reads up to the colon and stores what is before it, and does nothing at all with what's beyond it.
    [/edit]

    Quzah.
    Last edited by quzah; 08-02-2004 at 11:24 PM.
    Hope is the first step on the road to disappointment.

  4. #4
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    check this out and tell me if it helps at all

    Code:
    fscanf(infile,"%4[^,],%15[^,],%13[^,],%12[^,],"
    	   "%17[^,],%10[^,],%11[^,],%14[^,],"
    	   "%6[^,],%*c",
    	   label1,label2,
    	   label3,label4,
    	   label5,label6,
    	   label7,label8,
    	   label9);
    Last edited by xviddivxoggmp3; 08-02-2004 at 11:38 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Quote Originally Posted by quzah
    [edit]
    And on an aside, no, it doesn't do that at all. It reads up to the colon and stores what is before it, and does nothing at all with what's beyond it.
    [/edit]

    Quzah.
    Explain how the first "%[^:]" will extract and store a string but not the second?
    Or if you'd rather, explain why the following code prints
    Code:
    2
    abcd
    efgh
    using every sscanf() implementation I have access to.
    Code:
        char test[] = "abcd:efgh";
        char a[15], b[15];
    
        int res = sscanf(test, "%[^:]:%[^:]", a, b);
    
        printf("%d\n%s\n%s\n", res, a, b);
    Ah, good to be back on the boards.
    gg

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Just noticing that this isn't the C board - so here is the C++ equivalent which does not extract anything before the colon.
    Code:
    #include <sstream>
    #include <iostream>
    #include <string>
    #include <limits>
    using namespace std;
    
    int main()
    {
        istringstream in("abcd:efgh");
        string everything_after;
    
        if (in.ignore(numeric_limits<int>::max(), ':') &&
            getline(in, everything_after, ':'))
        {
            cout << "Everything after the colon: " << everything_after << endl;
        }//if
        else
        {
            cerr << "Failure while parsing" << endl;
        }//else
    
        return 0;
    }//main
    gg

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    27
    I never use cout, or any of the iostream files. Are they easier? o_0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf() question
    By NuNn in forum C Programming
    Replies: 7
    Last Post: 02-28-2009, 02:57 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. sscanf question
    By Little_Dump in forum C Programming
    Replies: 5
    Last Post: 10-27-2003, 02: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. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM