C:正确使用strtok_r [英] C: correct usage of strtok_r
问题描述
如何使用strtok_r代替strtok来执行此操作?
How can I use strtok_r instead of strtok to do this?
char *pchE = strtok(NULL, " ");
现在我想使用 strtok_r
正确...但有时我得到 strtol
的问题。
我有一个线程,我执行10次(同时)。
Now I'm trying to use strtok_r
properly... But sometimes I get problems with the strtol
.
I have a thread that I execute 10 times (at the same time).
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);
我正在捕捉 readNTurma == NULL
几次...为什么?不明白为什么会出现 NULL
?
And I'm catching that readNTurma == NULL
several times... Why is that? Cant understand why it comes NULL
?
推荐答案
strtok_r的文档非常清楚。
strtok_r()函数是一个可重入版本strtok()。 saveptr参数是指向由strtok_r()内部使用的char *变量的指针,以便在解析相同字符串的连续调用之间维护上下文。
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(),str应该指向要解析的字符串,并且忽略saveptr的值。在后续调用中,str应为NULL,并且saveptr应自上次调用后保持不变。
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.
所以你会有
char str[] = "Hello world";
char *saveptr;
char *foo, *bar;
foo = strtok_r(str, " ", &saveptr);
bar = strtok_r(NULL, " ", &saveptr);
这篇关于C:正确使用strtok_r的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!