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

cimon's Avatar, Join Date: Feb 2007
Go4Expert Member
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
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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.
wrecker's Avatar, Join Date: Mar 2007
Go4Expert Member
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...
DaWei's Avatar, Join Date: Dec 2006
Team Leader
The bracket format is NOT used by printf. Where would be the utility of such a thing? Please try to avoid disseminating bogus information.
rejishraghavan's Avatar, Join Date: Jan 2007
Newbie Member
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);
DaWei's Avatar, Join Date: Dec 2006
Team Leader
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.