sscanf
???C?????,??????????????
- ???????:?
sscanf
???????,???????????????????,?????????????????,????????:
int a, b; char input[] = "10 20"; sscanf(input, "%d %d", &a, &b);
- ??????:???????,????????????,??
%d
????,%f
?????,%s
?????????:
int a; float b; char input[] = "10 20.5"; sscanf(input, "%d %f", &a, &b);
- ??????:???????????????,??????????????
[]
???,?????????????,????????:
int a, b; char input[] = "10,20"; sscanf(input, "%d[,]%d", &a, &b);
- ???????:???????,?????
%n
(??n?????)????????????,????????10???????,????????:
char str[11]; char input[] = "Hello, World!"; sscanf(input, "s", str);
- ??????????:?????????????,???????????????????????????,???????"Name:"??????,????????:
char name[20]; char input[] = "Name: John Doe"; sscanf(input, "Name: s", name);
- ??????:?????????????????,????
%n
?????????????????:
int a, b; char input1[] = "10"; char input2[] = "10 20"; int count; sscanf(input1, "%d%n", &a, &count); if (count == strlen(input1)) { b = 0; // ????? } else { sscanf(input1 + count, " %d", &b); } sscanf(input2, "%d%n", &a, &count); if (count == strlen(input2)) { b = 0; // ????? } else { sscanf(input2 + count, " %d", &b); }
???????????????sscanf
??????????????????,sscanf
?????????????????????,??????????????