转载地址:/article/43744.htm
1.strtok介绍
众所周知,strtok可以根据用户所提供的分割符(同时分隔符也可以为复数比如",。")
将一段字符串分割直到遇到"\0".
比如,分隔符=“,” 字符串=“Fred,John,Ann”
通过strtok 就可以把3个字符串 “Fred” “John” “Ann”提取出来。
上面的C代码为
int in = 0;char buffer[] = "Fred,John,Ann"char *p[3];char *buff = buffer;while ((p[in] = strtok(buf, ",")) != NULL) {i++;buf = NULL;}
如上代码,第一次执行strtok需要以目标字符串的地址为第一参数(buf=buffer),之后srtok需要以NULL为第一参数(buf=NULL)。指针列p[],则储存了分割后的结果,p[0]="John", p[1] = "John", p[2]="Ann",而buf就变成Fred\0John\0Ann\0。
2.strtok的弱点
让我们更改一下我们的计划:我们有一段字符串 "Fred male 25,John male 62,Anna female 16" 我们希望把这个字符串整理输入到一个struct,
struct person {
char[25] name;
char[6] sex;
char[4] age;
}
要做到这个,其中一个方法就是先提取一段被","分割的字符串,然后再将其以" "(空格)分割。
比如:截取“Fred male 25”然后分割成"Fred" "male" "25"
以下我写了个小程序去表现这个过程:
#include <stdio.h>#include <string.h>#define INFO_MAX_SZ 255int main() {int in = 0;char buffer[INFO_MAX_SZ] = "Fred male 25,John male 62,Anna female 16";char *p[20];char *buf = buffer;while((p[in] = strtok(buf, ",")) != NULL) {buf = p[in];while((p[in] = strtok(buf, " ")) != NULL) {in++;buf = NULL;}p[in++] = "***"; //表现分割buf = NULL;}printf("Here we have %d strings\n", in);for (int j=0; j<in; j++)printf(">%s<\n", p[j]);return 0;}
这个程序输出为:
Here we have 4 strings
>Fred<
>male<
>25<
>***<
这只是一小段的数据,并不是我们需要的。但这是为什么呢? 这是因为strtok使用一个static(静态)指针来操作数据,让我来分析一下以上代码的运行过程:
红色为strtok的内置指针指向的位置,蓝色为strtok对字符串的修改
1."Fred male 25,John male 62,Anna female 16" //外循环
2."Fred male 25\0John male 62,Anna female 16" //进入内循环
3."Fred\0male 25\0John male 62,Anna female 16"
4."Fred\0male\025\0John male 62,Anna female 16"
5."Fred\0male\025\0John male 62,Anna female 16" //内循环遇到"\0"回到外循环
6."Fred\0male\025\0John male 62,Anna female 16" //外循环遇到"\0"运行结束
3.使用strtok_r
在这种情况我们应该使用strtok_r,strtok reentrant.
char *strtok_r(char *s, const char *delim, char **ptrptr);
相对strtok我们需要为strtok提供一个指针来操作,而不是像strtok使用配套的指针。
代码:
#include <stdio.h>#include <string.h>#define INFO_MAX_SZ 255int main(){int in=0;char buffer[INFO_MAX_SZ]="Fred male 25,John male 62,Anna female 16";char *p[20];char *buf = buffer;char *outer_ptr = NULL;char *inner_ptr = NULL;while((p[in] = strtok_r(buf, ",", &outer_ptr)) != NULL) {buf = p[in];while ((p[in] = strtok_r(buf, " ", &inner_ptr)) != NULL) {in++;buf = NULL;}p[in++] = "***";buf=NULL;}printf("Here we have %d strings\n", in);for (int j=0; j<in; j++) printf(">%s<\n", p[j]);return 0;}
这一次的输出为:
Here we have 12 strings
>Fred<
>male<
>25<
>***<
>John<
>male<
>62<
>***<
>Anna<
>female<
>16<
>***<
让我来分析一下以上代码的运行过程:
红色为strtok_r的outer_ptr指向的位置,
紫色为strtok_r的inner_ptr指向的位置,
蓝色为strtok对字符串的修改
1. "Fred male 25,John male 62,Anna female 16" //外循环
2. "Fred male 25\0John male 62,Anna female 16"//进入内循环
3. "Fred\0male 25\0John male 62,Anna female 16"
4 "Fred\0male\025\0John male 62,Anna female 16"
5 "Fred\0male\025\0John male 62,Anna female 16" //内循环遇到"\0"回到外循环
6 "Fred\0male\025\0John male 62\0Anna female 16"//进入内循环
原来, 该函数修改了原串.
所以,当使用char *test2 = "feng,ke,wei"作为第一个参数传入时,在位置①处, 由于test2指向的内容保存在文字常量区,该区的内容是不能修改的,所以会出现内存错误. 而char test1[] = "feng,ke,wei" 中的test1指向的内容是保存在栈区的,所以可以修改