好的,我知道strtok会修改其输入参数,但在这种情况下,它将输入字符串缩减为仅第一个标记。为什么会这样,我该怎么做才能解决这个问题?(请注意,我不是在谈论变量“temp”,它应该是第一个标记,而是变量“input”,在调用一次strtok后变为“this”)
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
char input[]="this is a test of the tokenizor seven";
char * temp;
temp=strtok(input," ");
printf("input: %s\n", input); //input is now just "this"
}
strtok
完成并返回NULL(因为没有更多的标记)之后,初始字符串是否会恢复?或者为了安全地使用strtok
,您应该复制源字符串吗?另外,如果在strtok
完成之前停止它,我的原始字符串会发生什么? - Cătălina Sîrbustrtok
在任何情况下都不会恢复原始字符串。如果它这样做了,它将使其创建的所有标记无效,这意味着您必须复制它们才能有用 - 但事实并非如此。 - Ernest Friedman-Hill