C语言 C: strtok_r 的正确用法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15961253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:01:10  来源:igfitidea点击:

C: correct usage of strtok_r

ccharstrtok

提问by Carlcox89

How can I use strtok_r instead of strtok to do this?

我如何使用 strtok_r 而不是 strtok 来做到这一点?

char *pchE = strtok(NULL, " ");

Now I'm trying to use strtok_rproperly... But sometimes I get problems with the strtol. I have a thread that I execute 10 times (at the same time).

现在我正在尝试strtok_r正确使用...但有时我会遇到strtol. 我有一个线程,我执行了 10 次(同时)。

char *savedEndd1;
char *nomeClass = strtok_r(lineClasses, " ", &savedEndd1);
char *readLessonS = strtok_r (NULL, " ", &savedEndd1);
char *readNTurma = strtok_r(NULL, " ",  &savedEndd1);

if (readNTurma==NULL)
printf("CLASS STRTOL begin %s %s\n",nomeClass, readLessonS );
int numberNTurma = strtol(readNTurma, NULL, 10);

And I'm catching that readNTurma == NULLseveral times... Why is that? Cant understand why it comes NULL?

而且我发现readNTurma == NULL了好几次......为什么会这样?不明白为什么会出现NULL

回答by tangrs

The documentationfor strtok_r is quite clear.

strtok_r的文档非常清楚。

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.

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() 函数是一个可重入版本的 strtok()。saveptr 参数是一个指向 char * 变量的指针,strtok_r() 在内部使用该变量,以便在解析相同字符串的连续调用之间维护上下文。

在第一次调用 strtok_r() 时,str 应该指向要解析的字符串,saveptr 的值被忽略。在后续调用中,str 应为 NULL,而 saveptr 应自上次调用以来保持不变。

So you'd have code like

所以你会有这样的代码

char str[] = "Hello world";
char *saveptr;
char *foo, *bar;

foo = strtok_r(str, " ", &saveptr);
bar = strtok_r(NULL, " ", &saveptr);

回答by MrHIDEn

Tested example:

测试示例:

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

int main(void)
{
    char str[] = "1,22,333,4444,55555";
    char *rest = NULL;
    char *token;

    for (token = strtok_r(str, ",", &rest);
         token != NULL;
         token = strtok_r(NULL, ",", &rest)) {   
        printf("token:%s\n", token);
    }

    return 0;
}

Result.

结果。

token:1
token:22
token:333
token:4444
token:55555

Test: http://codepad.org/6xRdIecI

测试:http: //codepad.org/6xRdIecI

From linux documentationwhere emphasis is mine:

linux 文档中,重点是我的:

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

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.

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.

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

strtok_r() 函数是一个可重入版本的 strtok()。saveptr 参数是一个指向 char * 变量的指针,strtok_r() 在内部使用该变量,以便在解析相同 string 的连续调用之间维护上下文

在第一次调用 strtok_r() 时, str 应该指向要解析的字符串,并且会忽略 saveptr 的值。在后续调用中, str 应该为 NULL,而 saveptr 应该自上次调用以来保持不变

可以使用指定不同 saveptr 参数的 strtok_r() 调用序列同时解析不同的字符串。

回答by Srinath Sureshkumar

I post a tested example to understand the correct usage of strtok_r() instead of using strtok() in nests.

我发布了一个经过测试的示例,以了解 strtok_r() 的正确用法,而不是在嵌套中使用 strtok()。

first lets take a string "y.o.u,a.r.e,h.e.r.e" and separate using the delimiters "," and "."

首先让我们取一个字符串“you,are,here”并使用分隔符“,”和“.”分开。

using strtok()

使用 strtok()

#include<stdio.h>
#include<string.h>
int main(void) {

        char str[]="y.o.u,a.r.e,h.e.r.e";
        const char *p=",", *q=".";
        char *a,*b;

        for( a=strtok(str,p) ; a!=NULL ; a=strtok(NULL,p) ) {
                printf("%s\n",a);
                for( b=strtok(a,q) ; b!=NULL ; b=strtok(NULL,q) )
                        printf("%s\n",b);
        }

        return 0;
}

OUTPUT:

输出:

y.o.u
y
o
u


ÿ
Ø
ü

now lets use strtok_r() for same example

现在让我们在同一个例子中使用 strtok_r()

using strtok_r()

使用 strtok_r()

#include<stdio.h>
#include<string.h>
int main(void) {

        char str[]="y.o.u,a.r.e,h.e.r.e";
        const char *p=",",*q=".";
        char *a,*b,*c,*d;

        for( a=strtok_r(str,p,&c) ; a!=NULL ; a=strtok_r(NULL,p,&c) ) {
                printf("%s\n",a);

                for( b=strtok_r(a,q,&d) ; b!=NULL ; b=strtok_r(NULL,q,&d) )
                        printf("%s\n",b);
        }

        return 0;
}

OUTPUT:

输出:

y.o.u
y
o
u
a.r.e
a
r
e
h.e.r.e
h
e
r
e


Ÿ
Ø
ü

一个
[R
ê
这里
^ h
é
[R
é

therefore the strtok_r() has reentrant property whereas strtok() doesnot function like that.

因此 strtok_r() 具有可重入属性,而 strtok() 没有那样的功能。

回答by BLUEPIXY

char str[]="string for sample";
char *reserve;
char *pchE = strtok_r(str, " ", &reserve);//when next call str -> NULL