本帖最后由 Ject 于 2023-9-1 23:20 编辑
如题:将字符串进行切割,输入const char * ,分隔符为[,],数据示例[234,211,222]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void parse_input(const char *input, int *a, int *b, int *c) {
char *input_copy = strdup(input);
char *token = strtok(input_copy, ",");
*a = atoi(token);
token = strtok(NULL, ",");
*b = atoi(token);
token = strtok(NULL, ",");
*c = atoi(token);
free(input_copy);
}
int main() {
const char *input = "255,254,250";
int a, b, c;
parse_input(input, &a, &b, &c);
printf("a: %d\n", a);
printf("b: %d\n", b);
printf("c: %d\n", c);
return 0;
}
办法千千万,欢迎大家用别的办法回帖。 |