字符串分割函數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:%s\n",token);
    printf("saveptr:%s\n\n\n",saveptr);
    
    
    token = strtok_r(saveptr, " ", &saveptr);
    printf("token:%s\n",token);
    printf("saveptr:%s\n",saveptr);
    
    return 0;    
}
結果:

 

 

 

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中包含的分割字符時,則會將該字符改為\0 字符。在第一次調用時,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 ( "%s\n" ,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 ( "%s\n" ,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 <<  "\n\nThe tokens are:\n\n" ;
     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是一個線程不安全的函數,因為它使用了靜態分配的空間來存儲被分割的字符串位置


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM