Thread: sscanf string match

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732

    sscanf string match

    Hi all,

    Quick one. I'm trying to match the following string and fetch everything until the three point. For example

    Code:
    Text which need to be read
     
    fdsafsdfdsa...
    fsdafsdaf.
    fsdafasdf...
    fsdafasd..
    And my sscaf function looks like this

    Code:
    if( sscanf( str, "%[^...]...", str1 ) == 1 )
      printf( str1 )
    But this donst work. It gets everything. I want to fetch string with end with '...'. So the string which i would be expecting is the 1st and 3rd row. But not the 2nd or 3rd since they end of '.' or '..' dots.

    The key here is to write a format specifier which tell to read everything until you see '...' but not just '.'. And tried all possible ways and couldn't work around with this. Is there anything i'm missing?

    thanks guys

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Qualify your string before you use sscanf() on it.

    strstr() can do that for you:

    pstr = str;

    pstr = strstr(str, "...");

    if(pstr)
    /* the string has 3 dots in it */
    //proceed with your sscanf(), but remove the ... part from it.
    else
    /* the string doesn't have 3 dots in a row */
    //whatever logic you need for that case


    strstr() returns NULL if the target substring is not found.

    You'll need to include string.h, of course.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    sscanf( buf, "...%[^\n]", foo );
    Something like that should work.
    Or, as Adak mentioned, strstr:
    Code:
    if( (p = strstr( foo, "..." )) )
        strcpy( buf, p + 3 );
    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM