我想知道如何从字符串中提取各种数字。我知道 strtol 有效,但它似乎只适用于第一个数字。
这是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(){
long v1, v2, v3;
char str[20] = "16,23";
char *d;
v1 = strtol(str, &d, 10);
v2 = strtol(str, &d, 10);
printf("string is %s\nv1 is:%i\nv2 is:%d\n",str , v1,v2);
return 0;
}
在这个例子中,我想输出 v1 = 16 和 v2 = 23。
另一个例子,如果 str 是“12,23,34”,我想要 v3=34
提前致谢 :)