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

Go4Expert Member
14Apr2007,19:44   #1
cimon's Avatar
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
Team Leader
14Apr2007,20:07   #2
DaWei's Avatar
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.
Go4Expert Member
15Apr2007,14:34   #3
wrecker's Avatar
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...
Team Leader
15Apr2007,16:41   #4
DaWei's Avatar
The bracket format is NOT used by printf. Where would be the utility of such a thing? Please try to avoid disseminating bogus information.
Newbie Member
17Apr2007,21:33   #5
rejishraghavan's Avatar
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);
Team Leader
17Apr2007,22:40   #6
DaWei's Avatar
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]",char);
char is a type, not your pointer.