Linux C编程——strtok_r 和 strtok 函数

 

 

一、简介

 

NAME
       strtok, strtok_r - extract tokens from strings

SYNOPSIS
       #include <string.h>

       char *strtok(char *str, const char *delim);

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

DESCRIPTION
       The strtok() function parses a string into a sequence of tokens.  On the first call to strtok() the string to be parsed should be specified in str.  In each subsequent call that should parse the same  string,  str should be NULL.

        解析str指向的字符串,delim指向分隔符,后续调用解析相同的字符串时,str设置为NULL。

       The delim argument specifies a set of characters that delimit the tokens in the parsed string.  The caller may specify different strings in delim in successive calls that parse the same string.

        后续解析相同字符串时,分隔符delim可以不同。

       Each call to strtok() returns a pointer to a null-terminated  string  containing  the  next  token.   This  string does not include the delimiting character.  If no more tokens are found, strtok() returns NULL.

        strtok 返回以NULL结尾的指向后续字符的指针,指针指向的字符串不包含下一个分隔符delim及其后面的字符。解析到结尾时,返回NULL。

       A  sequence of two or more contiguous delimiter characters in the parsed string is considered to be a single delimiter.  Delimiter characters at the start or end of the string are ignored.  Put another way:  the tokens returned by strtok() are always non-empty strings.

       待解析字符串str中连续的多个delim被认为只有一个,str中开头和结尾的delim被忽略,运行例子如下:
    [root@localhost tmp]# ./a.out "||a,b,c||e,d,f|h,i,j|w,z,d,e" "|" ','  
    1: a,b,c --> a --> b --> c
    2: e,d,f --> e --> d --> f
    3: h,i,j --> h --> i --> j
    4: w,z,d,e --> w --> z --> d --> e

        The  strtok_r()  function  is a reentrant version strtok().  The saveptr argument is a pointer to a char * variable that is used internally by strtok_r() in order to maintain context between successive calls  that parse the same string.

        strtok_r是strtok的可重入版。

       On  the first call to strtok_r(), str should point to the string to be parsed, and the value of saveptr is ignored.  In subsequent calls, str should be NULL, and saveptr should  be  unchanged  since  the  previous call.
        
        首次调用strtok_r时,str设置为待解析字符串,saveptr 被忽略,后续调用str设为NULL,saveptr 不变,用来连接上次的解析。

       Different strings may be parsed concurrently using sequences of calls to strtok_r() that specify different saveptr arguments.

        如例子中所示,saveptr 不同,表示解析不同的字符串。

 

二、举例

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(int argc, char *argv[])
{
char *str1, *str2, *token, *subtoken;
char *saveptr1, *saveptr2;
int j;

if (argc != 4) {
        fprintf(stderr, "Usage: %s string delim subdelim\n",
          argv[0]);
        exit(EXIT_FAILURE);
}

for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
        token = strtok_r(str1, argv[2], &saveptr1);
        if (token == NULL)
                break;
        printf("%d: %s", j, token);

        for (str2 = token; ; str2 = NULL) {
                subtoken = strtok_r(str2, argv[3], &saveptr2);
                if (subtoken == NULL)
                        break;
                printf(" --> %s", subtoken);
        }
        puts("");
}

exit(EXIT_SUCCESS);
} /* main */

测试结果:

[root@localhost tmp]# gcc strtok_r.c                             
[root@localhost tmp]# ./a.out "a,b,c|e,d,f|h,i,j|w,z,d,e" '|' ','
1: a,b,c --> a --> b --> c
2: e,d,f --> e --> d --> f
3: h,i,j --> h --> i --> j
4: w,z,d,e --> w --> z --> d --> e

[root@localhost tmp]# ./a.out "||a,b,c||e,d,f|h,i,j|w,z,d,e" "|" ','  
1: a,b,c --> a --> b --> c
2: e,d,f --> e --> d --> f
3: h,i,j --> h --> i --> j
4: w,z,d,e --> w --> z --> d --> e

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值