为什么 strtok 会这样改变它的输入?

发布于 2025-01-07 21:05:17 字数 461 浏览 0 评论 0原文

好的,所以我知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无悔心 2025-01-14 21:05:17

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 a NULL 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 stops printf() from showing it. If you used a for-loop to walk over the original input string up to the original number of characters, you'd find the data is all still there.

紫轩蝶泪 2025-01-14 21:05:17

您应该打印从 strtok 收到的标记,而不用担心输入数组,因为 strtok 将插入 NULL。您需要重复调​​用才能获取所有令牌:

#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," ");
  while( temp != NULL ) {
    printf("temp is \"%s\"\n", temp );
    temp = strtok( NULL, " ");
  }
}

You should printout the token that you receive from strtok and not worry about the input array because NULLs will be inserted by strtok. You need repeated calls to get all of the tokens:

#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," ");
  while( temp != NULL ) {
    printf("temp is \"%s\"\n", temp );
    temp = strtok( NULL, " ");
  }
}
自演自醉 2025-01-14 21:05:17

这是因为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文