STRTOK函数和STRTOK_R函数

1.strtok一个应用实例

需从字符串 charbuffer[INFO_MAX_SZ]="Fred male 25,John male 62,Anna female 16";中提取出人名、性别以及年龄。

strtok函数使用了静态空间保存每次切分后的一个指针,该指针指向剩余待切分字符串的第一个字符,且该指针为全局指针。


我给出了一种解决办法。同时以 ',’(逗号)和 ' ’(空格)为分界符,一层循环解决问题。

<span style="font-family:SimSun;">1. in = 0;  
2. while ((p[in] = strtok(buf, " ,")) != NULL)  
3. {  
4.     in++;  
5.     buf = NULL;  
6. }  
7. printf("Here we have %d strings/n", in);  
8. for (int j=0; j<in; j++)  
9. {     
10.     printf(">%s</n",p[j]);  
11. }  </span>


strtok是一个线程不安全的函数,推荐使用线程安全函数strtok_r

 

2.strtok_r及其使用

strtok_r是linux平台下的strtok函数的线程安全版。windows的string.h中并不包含它。要想使用这个函数,上网搜其linux下的实现源码,复制到你的程序中即可。别的方式应该也有,比如使用GNU CLibrary。我下载了GNU CLibrary,在其源代码中找到了strtok_r的实现代码,复制过来。可以看作是第一种方法和第二种方法的结合。

strtok的函数原型为 char *strtok_r(char *str, const char *delim,char **saveptr);

下面对strtok的英文说明摘自http://www.linuxhowtos.org/manpages/3/strtok_r.htm,译文是由我给出的。

strtok_r函数是strtok函数的可重入版本。char **saveptr参数是一个指向char *的指针变量,用来在strtok_r内部保存切分时的上下文,以应对连续调用分解相同源字符串。

第一次调用strtok_r时,str参数必须指向待提取的字符串,saveptr参数的值可以忽略。连续调用时,str赋值为NULL,saveptr为上次调用后返回的值,不要修改。

Different strings may be parsed concurrently using sequences ofcalls to strtok_r() that specify differentsaveptr arguments.

一系列不同的字符串可能会同时连续调用strtok_r进行提取,要为不同的调用传递不同的saveptr参数。

The strtok() function uses a static bufferwhile parsing, so it's not thread safe. Use strtok_r()if this matters to you.

strtok函数在提取字符串时使用了静态缓冲区,因此,它是线程不安全的。如果要顾及到线程的安全性,应该使用strtok_r。

 

strtok_r实际上就是将strtok内部隐式保存的this指针,以参数的形式与函数外部进行交互。由调用者进行传递、保存甚至是修改。需要调用者在连续切分相同源字符串时,除了将str参数赋值为NULL,还要传递上次切分时保存下的saveptr

举个例子,还记得前文提到的提取结构体的例子么?我们可以使用strtok_r,以双重循环的形式提取出每个人的信息。

1. int in=0;  

2. char buffer[INFO_MAX_SZ]="Fred male 25,John male 62,Anna female 16";  

3. char *p[20];  

4. char *buf=buffer;  

5. char *outer_ptr=NULL;  

6. char *inner_ptr=NULL;  

7. while((p[in] = strtok_r(buf, ",", &outer_ptr))!=NULL)   

8. {  

9.     buf=p[in];  

10.     while((p[in]=strtok_r(buf, " ", &inner_ptr))!=NULL)   

11.     {  

12.         in++;  

13.         buf=NULL;  

14.     }  

15.     buf=NULL;  

16. }  

17. printf("Here we have %d strings/n",in);  

18. for (int j=0; j<in; j++)  

19. {     

20.     printf(">%s</n",p[j]);  

21. }  


调用strtok_r的代码比调用strtok的代码多了两个指针,outer_ptrinner_ptrouter_ptr用于标记每个人的提取位置,即外循环;inner_ptr用于标记每个人内部每项信息的提取位置,即内循环。具体过程如下:

1)第1次外循环,outer_ptr忽略,对整个源串提取,提取出"Fred male25",分隔符','被修改为了'/0’outer_ptr返回指向'J’

2)第一次内循环,inner_ptr忽略对第1次外循环的提取结果"Fred male 25"进行提取,提取出了"Fred",分隔符' '被修改为了'/0'inner_ptr返回指向'm'

3)第二次内循环,传递第一次内循环返回的inner_ptr,第一个参数为NULL,从inner_ptr指向的位置'm'开始提取,提取出了"male",分隔符  ' '被修改为了'/0'inner_ptr返回指向'2'

4)第三次内循环,传递第二次内循环返回的inner_ptr,第一个参数为NULL,从inner_ptr指向的位置'2'开始提取,提取出了"25",因为没有找到' 'inner_ptr返回指向25后的'/0'

5)第四次内循环,传递第三次内循环返回的inner_ptr,第一个参数为NULL,因为inner_ptr指向的位置为'/0',无法提取,返回空值。结束内循环。

6)第2次外循环,传递第1次外循环返回的outer_ptr,第一个参数为NULL,从outer_ptr指向的位置'J'开始提取,提取出"John male 62",分隔符',’被修改为了'/0’outer_ptr返回指向'A’。(调用strtok则卡死在了这一步

……以此类推,外循环一次提取一个人的全部信息,内循环从外循环的提取结果中,二次提取个人单项信息。

可以看到strtok_r将原内部指针显示化,提供了saveptr这个参数。增加了函数的灵活性和安全性。

 

3.strtokstrtok_r的源代码

这两个函数的实现,由众多的版本。我strtok_r来自于GNU C Librarystrtok则调用了strtok_r。因此先给出strtok_r的源代码。

1. /* 

2.  * strtok_r.c: 

3.  * Implementation of strtok_r for systems which don't have it. 

4.  * 

5.  * This is taken from the GNU C library and is distributed under the terms of 

6.  * the LGPL. See copyright notice below. 

7.  * 

8.  */  

9.   

10. #ifdef HAVE_CONFIG_H   

11. #include "configuration.h"   

12. #endif /* HAVE_CONFIG_H */   

13.   

14. #ifndef HAVE_STRTOK_R   

15.   

16. static const char rcsid[] = "$Id: strtok_r.c,v 1.1 2001/04/24 14:25:34 chris Exp $";  

17.   

18. #include <string.h>   

19.   

20. #undef strtok_r   

21.   

22. /* Parse S into tokens separated by characters in DELIM. 

23.    If S is NULL, the saved pointer in SAVE_PTR is used as 

24.    the next starting point.  For example: 

25.         char s[] = "-abc-=-def"; 

26.         char *sp; 

27.         x = strtok_r(s, "-", &sp);      // x = "abc", sp = "=-def" 

28.         x = strtok_r(NULL, "-=", &sp);  // x = "def", sp = NULL 

29.         x = strtok_r(NULL, "=", &sp);   // x = NULL 

30.                 // s = "abc/0-def/0" 

31. */  

32. char *strtok_r(char *s, const char *delim, char **save_ptr) {  

33.     char *token;  

34.   

35.     if (s == NULL) s = *save_ptr;  

36.   

37.     /* Scan leading delimiters.  */  

38.     s += strspn(s, delim);  

39.     if (*s == '/0')   

40.         return NULL;  

41.   

42.     /* Find the end of the token.  */  

43.     token = s;  

44.     s = strpbrk(token, delim);  

45.     if (s == NULL)  

46.         /* This token finishes the string.  */  

47.         *save_ptr = strchr(token, '/0');  

48.     else {  

49.         /* Terminate the token and make *SAVE_PTR point past it.  */  

50.         *s = '/0';  

51.         *save_ptr = s + 1;  

52.     }  

53.   

54.     return token;  

55. }   

代码整体的流程如下:

1)判断参数s是否为NULL,如果是NULL就以传递进来的save_ptr作为起始分解位置;若不是NULL,则以s开始切分。

2)跳过待分解字符串开始的所有分界符。

3)判断当前待分解的位置是否为'/0',若是则返回NULL(联系到(一)中所说对返回值为NULL的解释);不是则继续。

4)保存当前的待分解串的指针token,调用strpbrktoken中找分界符:如果找不到,则将save_ptr赋值为待分解串尾部'/0'所在的位置,token没有发生变化;若找的到则将分界符所在位置赋值为'/0'token相当于被截断了(提取出来),save_ptr指向分界符的下一位。

5)函数的最后(无论找到还是没找到)都将返回。

对于函数strtok来说,可以理解为用一个内部的静态变量将strtok_r中的save_ptr给保存起来,对调用者不可见。其代码如下:

1. char *strtok(char *s, const char *delim)  

2. {  

3.     static char *last;  

4.   

5.     return strtok_r(s, delim, &last);  

6. }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值