strtok()函数不能直接处理const char*, 用strncpy得到一个char型数组, 对这个temp数组进行处理即可。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
char *string = "T01:91+:123";
char tmp[16];
char* s;
/* Strtok can not do with const char*, so I use strncpy copy it to an char array */
strncpy(tmp, string, sizeof(string));
s = strtok (tmp, "T");
printf("string: %s\n", string); // 01:91+:123
s = strtok (s, ":");
printf("city id: %s\n", s); // 01
return 0;
}
----------
gist: https://gist.github.com/wusuopubupt/64cd0cb29bf67ed82af4
Update:
------------
#include<string.h>
#include<stdio.h>
int main(void)
{
char input[16]="abc,d";
char *p;
p=strtok(input,",");
if(p) printf("%s\n",p);
p=strtok(NULL,","); //注意这里!
if(p) printf("%s\n",p);
return 0;
}
输出:
abc
d