字符串分割函数strtok(线程不安全),线程安全函数strtok_r

strtok_r函数---字符串分割函数

函数原型:

       char *strtok_r(char *str, const char *delim, char **saveptr);

参数:

str:被分割的字符串,若str为NULL,则被分割的字符串为*saveptr

delim:依据此字符串分割str

saveptr:分割后剩余部分的字符串

返回值:

遇到第一个delim时,分割出的字符串,若没有遇到delim,则范围NULL

例程:

int main(int argc,char* argv[])
{
    char str[1024] = "this is a test!";
    char *token;
    char *saveptr;
    
    token = strtok_r(str, " ", &saveptr);
    printf("token:%sn",token);
    printf("saveptr:%snnn",saveptr);
    
    
    token = strtok_r(saveptr, " ", &saveptr);
    printf("token:%sn",token);
    printf("saveptr:%sn",saveptr);
    
    return 0;    
}
结果:

字符串分割函数strtok(线程不安全),线程安全函数strtok_r

 

 

 

3.strtok和strtok_r的源代码

这两个函数的实现,由众多的版本。我strtok_r来自于GNU C Library,strtok则调用了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,调用strpbrk在token中找分界符:如果找不到,则将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. }  

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delim中包含的分割字符时,则会将该字符改为 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针

c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<string.h>
#include<stdio.h>
int main(void)
{
    char input[16]="abc,d";
    char*p;
    /*strtok places a NULL terminator
    infront of the token,if found*/
    p=strtok(input,",");
    if(p)
        printf("%sn",p);
        /*Asecond 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("%sn",p);
    return 0;
}
c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    char sentence[]="This is a sentence with 7 tokens";
    cout << "The string to be tokenized is:n" << sentence << "nnThe tokens are:nn";
    char *tokenPtr=strtok(sentence," ");
    while(tokenPtr!=NULL) {
        cout<<tokenPtr<<endl;
        tokenPtr=strtok(NULL," ");
    }
    //cout << "After strtok,sentence=" << tokenPtr<<endl;
    return 0;
}
函数第一次调用需设置两个参数。第一次分割的结果,返回串中第一个 ',' 之前的字符串,也就是上面的程序第一次输出abc。
第二次调用该函数strtok(NULL,","),第一个参数设置为NULL。结果返回分割依据后面的字串,即第二次输出d。
strtok是一个线程不安全的函数,因为它使用了静态分配的空间来存储被分割的字符串位置

原文链接: https://www.cnblogs.com/alix-1988/p/12174626.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    字符串分割函数strtok(线程不安全),线程安全函数strtok_r

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/401073

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月17日 上午9:37
下一篇 2023年4月17日 上午9:38

相关推荐