为什么 strtok 会这样改变它的输入?
好的,所以我知道 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"
}
Ok, so I understand that strtok modifies its input argument, but in this case, it's collapsing down the input string into only the first token. Why is this happening, and what can I do to fix it? (Please note, I'm not talking about the variable "temp", which should be the first token, but rather the variable "input", which after one call to strtok becomes "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"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当
strtok()
找到一个token时,它会将紧随该token后面的字符更改为\0
,然后返回一个指向该token的指针。下次您使用NULL
参数调用它时,它会开始查找终止第一个标记的分隔符 - 即在\0
之后,甚至可能更远。现在,指向字符串开头的原始指针仍然指向字符串的开头,但第一个标记现在以
\0
结尾 - 即printf()
认为标记的结尾就是字符串的结尾。其余数据仍然存在,但\0
阻止了printf()
显示它。如果您使用 for 循环遍历原始输入字符串直至原始字符数,您会发现数据仍然存在。When
strtok()
finds a token, it changes the character immediately after the token into a\0
, and then returns a pointer to the token. The next time you call it with aNULL
argument, it starts looking after the separators that terminated the first token -- i.e., after the\0
, and possibly further along.Now, the original pointer to the beginning of the string still points to the beginning of the string, but the first token is now
\0
-terminated -- i.e.,printf()
thinks the end of the token is the end of the string. The rest of the data is still there, but that\0
stopsprintf()
from showing it. If you used afor
-loop to walk over the original input string up to the original number of characters, you'd find the data is all still there.您应该打印从
strtok
收到的标记,而不用担心输入数组,因为strtok
将插入 NULL。您需要重复调用才能获取所有令牌:You should printout the token that you receive from
strtok
and not worry about the input array because NULLs will be inserted bystrtok
. You need repeated calls to get all of the tokens:这是因为 strtok 会在每个分隔符中插入空值,这就是您使用重复调用 strtok 来获取每个标记的原因。一旦开始使用 strtok,输入字符串就无法使用。你不需要“修复”它——这就是它的工作原理。
It's because strtok inserts nulls into each separator, which is why you use repeated calls to strtok to get each token. The input string cannot be used once you start using strtok. You don't "fix" it -- this is how it works.