Sscanf c ++ format

I have sscanf instruction as

sscanf (fieldname, "%s_%d", name, id);

I give input as frog_461, but it displays the name as "frog_461" and 0 for id. Can you suggest the right way to contribute to make this expression work? As in the example above, how should I give my input so that name = "frog" and id = 461. Thank you.


I appreciate all your input. Currently I cannot change the code, so I am not trying to find an alternative way to make it work. I just check if this code worked before, and if so, what input should the user give in order to make it work. Thank.

+5
source share
4 answers

, , sscanf, ! sscanf:

sscanf(fieldname, "%[^_]_%d", name, &id); 

%[^_] , . . [ man scanf.

id id, - C pass by value.

, C, ++, , , . ++, , sscanf.

+13

%s , , , . strchr. , , %d.

+1

, , "" ( ) ,

char name[32];
memset (name, 0, sizeof(name));
if (sscanf(fieldname, "%31[a-z]_%d", name, num)>=2) {
   /* do something */
} else {
   /* bad fieldname */
}

sscanf C (.. char) ; .

+1

scanf , .

strtok() , , _ ""

-1

All Articles