Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   what "%[^_]_" this refers to in sscanf (http://www.go4expert.com/forums/refers-sscanf-t3868/)

cimon 14Apr2007 19:44

what "%[^_]_" this refers to in sscanf
 
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 14Apr2007 20:07

Re: what "%[^_]_" this refers to in sscanf
 
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 15Apr2007 14:34

Re: what "%[^_]_" this refers to in sscanf
 
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 15Apr2007 16:41

Re: what "%[^_]_" this refers to in sscanf
 
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 17Apr2007 21:33

Re: what "%[^_]_" this refers to in sscanf
 
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 17Apr2007 22:40

Re: what "%[^_]_" this refers to in sscanf
 
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.


All times are GMT +5.5. The time now is 02:42.