mysql strtok_c函数: strtok 和 strtok_r 详解
函數(shù)名: ? strtok
功 ? ? 能: ? 查找由在第二個(gè)串中指定的分界符分隔開的單詞
用 ? ? 法: ? char ? *strtok(char ? *str1, ? char ? *str2);
程序例:
#include ?
#include ?
int ? main(void)
{
char ? input[16] ? = ? "abc,d";
char ? *p;
/* ? strtok ? places ? a ? NULL ? terminator
in ? front ? of ? the ? token, ? if ? found ? */
p ? = ? strtok(input, ? ",");
if ? (p) ? ? ? printf("%s\n", ? p);
/* ? A ? second ? call ? to ? strtok ? using ? a ? NULL
as ? the ? first ? parameter ? returns ? a ? pointer
to ? the ? character ? following ? the ? token ? ? */
p ? = ? strtok(NULL, ? ",");
if ? (p) ? ? ? printf("%s\n", ? p);
return ? 0;
}
帶有_r的函數(shù)主要來自于UNIX下面。所有的帶有_r和不帶_r的函數(shù)的區(qū)別的是:帶_r的函數(shù)是線程安全的,r的意思是reentrant,可重入的。
上述程序運(yùn)行的結(jié)果是
abc
d
1. strtok介紹眾所周知,strtok可以根據(jù)用戶所提供的分割符(同時(shí)分隔符也可以為復(fù)數(shù)比如“,。”)
將一段字符串分割直到遇到"\0".
比如,分隔符=“,” 字符串=“Fred,John,Ann”
通過strtok 就可以把3個(gè)字符串 “Fred”???? “John”????? “Ann”提取出來。
上面的C代碼為
QUOTE:
int in=0;
char buffer[]="Fred,John,Ann"
char *p[3];
char *buff = buffer;
while((p[in]=strtok(buf,","))!=NULL) {
i++;
buf=NULL; }
如上代碼,第一次執(zhí)行strtok需要以目標(biāo)字符串的地址為第一參數(shù)(buf=buffer),之后strtok需要以NULL為第一參數(shù)(buf=NULL)。指針列p[],則儲存了分割后的結(jié)果,p[0]="John",p[1]="John",p[2]="Ann",而buf就變成??? Fred\0John\0Ann\0。
2. strtok的弱點(diǎn)
讓我們更改一下我們的計(jì)劃:我們有一段字符串 "Fred male 25,John male 62,Anna female 16" 我們希望把這個(gè)字符串整理輸入到一個(gè)struct,
QUOTE:
struct person {
char [25] name ;
char [6] sex;
char [4] age;
}
要做到這個(gè),其中一個(gè)方法就是先提取一段被“,”分割的字符串,然后再將其以“ ”(空格)分割。
比如: 截取 "Fred male 25" 然后分割成 "Fred" "male" "25"
以下我寫了個(gè)小程序去表現(xiàn)這個(gè)過程:
QUOTE:
#include
#include
#define INFO_MAX_SZ 255
int 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++]="***"; //表現(xiàn)分割
buf=NULL; }
printf("Here we have %d strings\n",i);
for (int j=0; j
printf(">%s
return 0;
}
這個(gè)程序輸出為:
Here we have 4 strings
>Fred<
>male<
>25<
>***<
這只是一小段的數(shù)據(jù),并不是我們需要的。但這是為什么呢? 這是因?yàn)閟trtok使用一個(gè)static(靜態(tài))指針來操作數(shù)據(jù),讓我來分析一下以上代碼的運(yùn)行過程:
紅色為strtok的內(nèi)置指針指向的位置,藍(lán)色為strtok對字符串的修改
1. "Fred male 25,John male 62,Anna female 16" //外循環(huán)
2. "Fred male 25\0John male 62,Anna female 16" //進(jìn)入內(nèi)循環(huán)
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" //內(nèi)循環(huán)遇到"\0"回到外循環(huán)
6?? "Fred\0male\025\0John male 62,Anna female 16" //外循環(huán)遇到"\0"運(yùn)行結(jié)束。
3. 使用strtok_r
在這種情況我們應(yīng)該使用strtok_r, strtok reentrant.
char *strtok_r(char *s, const char *delim, char **ptrptr);
相對strtok我們需要為strtok提供一個(gè)指針來操作,而不是像strtok使用配套的指針。
代碼:
QUOTE:
#include
#include
#define INFO_MAX_SZ 255
int 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",i);
for (int j=0; jn
printf(">%s
return 0;
}
這一次的輸出為:
Here we have 12 strings
>Fred<
>male<
>25<
>***<
>John<
>male<
>62<
>***<
>Anna<
>female<
>16<
>***<
讓我來分析一下以上代碼的運(yùn)行過程:
紅色為strtok_r的outer_ptr指向的位置,
紫色為strtok_r的inner_ptr指向的位置,
藍(lán)色為strtok對字符串的修改
1. "Fred male 25,John male 62,Anna female 16" //外循環(huán)
2. "Fred male 25\0John male 62,Anna female 16"//進(jìn)入內(nèi)循環(huán)
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" //內(nèi)循環(huán)遇到"\0"回到外循環(huán)
6?? "Fred\0male\025\0John male 62\0Anna female 16"//進(jìn)入內(nèi)循環(huán)
注:
屬轉(zhuǎn)載
文章轉(zhuǎn)載連接:http://blog.chinaunix.net/u2/66402/showart_1168731.html
總結(jié)
以上是生活随笔為你收集整理的mysql strtok_c函数: strtok 和 strtok_r 详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: django-form and fiel
- 下一篇: 用英伟达 DIGITS 进行图像分割