1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

what "%[^_]_" this refers to in sscanf

Discussion in 'C' started by cimon, Apr 14, 2007.

  1. cimon

    cimon New Member

    Joined:
    Feb 9, 2007
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    Hi

    can some exaplin me wat WHAT and HOW this sacanf function is working

    sscanf(NAME(img), "%[^_]_%[^_]_%[^_]_%[^_]_%d.%[^_]",
    userid, head, expression, eyes, &scale, photo);

    its used here

    Code:
    load_target(img, net)
    IMAGE *img;
    BPNN *net;
    {
      int scale;
      char userid[40], head[40], expression[40], eyes[40], photo[40];
    
      userid[0] = head[0] = expression[0] = eyes[0] = photo[0] = '\0';
    
      /*** scan in the image features ***/
      sscanf(NAME(img), "%[^_]_%[^_]_%[^_]_%[^_]_%d.%[^_]",
        userid, head, expression, eyes, &scale, photo);
    
      if (!strcmp(userid, "glickman")) {
        net->target[1] = TARGET_HIGH;  /* it's me, set target to HIGH */
      } else {
        net->target[1] = TARGET_LOW;   /* not me, set it to LOW */
      }
    }
    
    
    NAME(img) is nothing but this

    Code:
    #define NAME(img)   ((img)->name)
    and img is a structure

    i understand the functioning of scanf like in this
    Code:
    sscanf(line, "%d %d", &nc, &nr);
    "line" is string and from it it retrieves integer and store it in nc and nr

    BUT i am not able to understand what wil this refer to %[^_]_ and what wil it store in userid and ither mentioned as in

    sscanf(NAME(img), "%[^_]_%[^_]_%[^_]_%[^_]_%d.%[^_]",
    userid, head, expression, eyes, &scale, photo);

    Thankyou
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    The square brackets are a special form of the 's' specifier. They are similar to square brackets used in a regex expression. They define a class of characters for scanf to match. For example, %[abc] asks scanf to capture the character if it is an 'a' OR a 'b' OR a 'c', and stash it in our specified variable. The '^' character has the effect of inverting the request. For example, %[^abc] asks scanf to capture the character if it is NOT an 'a' or 'b' or 'c'.

    This is documented in your material regarding 'scanf'. You might want to read that.
     
  3. wrecker

    wrecker New Member

    Joined:
    Mar 12, 2007
    Messages:
    40
    Likes Received:
    0
    Trophy Points:
    0
    All that i know about %[^_]is that it is used to scan a particular line includeing the blank spaces or nul values..... This syntax is used in printf..... Also DaWei has given a very good and explanation..... I loved it...
     
  4. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    The bracket format is NOT used by printf. Where would be the utility of such a thing? Please try to avoid disseminating bogus information.
     
  5. rejishraghavan

    rejishraghavan New Member

    Joined:
    Jan 17, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Using this [^] (^ caret Symbol ) we can get the input till the character from the keyboard....

    for Example....

    U can get a line(Till \n is reached) by using Scanf insted of gets();
    U can give any character [^ #] ....[^_]


    char *a=(char*)malloc(sizeof(char));
    scanf("%[^\n]",char);
     
  6. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    Unfortunately, your code will trash the heap. You only malloc'ed one char. Scanf will return anything entered BUT the newline. Malloc for a single char is hideously inefficient, and the overhead for managing that memory will be many times as large as the single char. Additionally, you have an error here:
    Code:
    scanf("%[^\n]",[COLOR=Red]char[/COLOR]);
    
    char is a type, not your pointer.
     

Share This Page