strtok() 获取字符串的第一个标记时出现问题

发布于 2024-11-23 23:22:55 字数 233 浏览 2 评论 0原文

以下代码无法获取字符串的第一个标记:

char *p1;
char array[100];
strcpy(array, "ANY STRING WOULD DO");
p1 = strtok(array, " ");
p1 = strtok(NULL, " ");
p1 = strtok(array, " ");
p1 = strtok(NULL, " ");

printf("%c", p1);

The following code fails to get me the first token of the string:

char *p1;
char array[100];
strcpy(array, "ANY STRING WOULD DO");
p1 = strtok(array, " ");
p1 = strtok(NULL, " ");
p1 = strtok(array, " ");
p1 = strtok(NULL, " ");

printf("%c", p1);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

囍笑 2024-11-30 23:22:55

strtok() 实际上用文字“\0”替换分隔符 - 实际上是覆盖原始字符串。第二次调用 strtok(array, ""); 时,array 现在看起来像

ANY'\0'STRING'\0' 都可以

(例如,如果您要打印字符串,您只会看到“ANY”)

由于 strtok 不会超出字符串末尾,因此您第二次只会获得一个标记,并且调用 strtok(NULL, “”); 返回NULL。要解决您的问题,您需要将字符串复制到另一个位置以进行第二组操作,或者保存临时字符串指针。

strtok() actually replaces the delimiters with a literal '\0' - in effect writing over your original string. The second time you call strtok(array, "");, array now looks like

ANY'\0'STRING'\0'WOULD DO

(e.g. if you were to print the string, you'd just see "ANY")

Since strtok doesn't go beyond the end of a string, you'll only get the one token the second time around, and the call to strtok(NULL, " "); returns NULL. To overcome your problem, you need to either copy the string into another location for the second set of operations, or save a temporary string pointer.

冰魂雪魄 2024-11-30 23:22:55

请查看此处给出的示例: MSDN: strtok , wcstok, _mbstok

Please take a look at the example given here: MSDN: strtok, wcstok, _mbstok

江南月 2024-11-30 23:22:55

这可行:

char *p1;
char array[100];
strcpy(array, "ANY STRING WOULD DO");
p1 = strtok(array, " ");
p1 = strtok(NULL, " ");
strcpy(array, "ANY STRING WOULD DO");
p1 = strtok(array, " ");

printf("%s", p1);

strtok 修改它传递的字符串,因此如果您想重新解析它,您需要再次复制它。

如果您只想保留令牌,只需复制指针即可。

This would work:

char *p1;
char array[100];
strcpy(array, "ANY STRING WOULD DO");
p1 = strtok(array, " ");
p1 = strtok(NULL, " ");
strcpy(array, "ANY STRING WOULD DO");
p1 = strtok(array, " ");

printf("%s", p1);

strtok modifies the string it is passed so if you want to re-parse it you need to copy it again.

If you just want to keep the tokens, just copy the pointers.

回忆凄美了谁 2024-11-30 23:22:55

请记住,strtok 通过在每个标记后面放置 '\0' 字符来修改字符串。

因此,当您尝试再次标记同一字符串时,实际上只是标记了第一个标记。

这将导致第二个 p1 = strtok(NULL, " "); 将返回 NULL,当您尝试打印 p1 时,它会尝试取消引用NULL,并且可能会失败。

请注意,您需要使用 "%s" 而不是 "%c",因为您打印的是字符串,而不是字符。

Remember that strtok modifies the string by placing '\0' characters after every token.

So, when you try tokenizing the same string again, you're really tokenizing the first token only.

This will have as a result that the second p1 = strtok(NULL, " "); will return NULL, and when you then try to print p1, it'll try to dereference NULL, and probably fail.

Note btw, that you need to use "%s" instead of "%c", because you're printing a string, not a character.

愚人国度 2024-11-30 23:22:55

strtok() 实际上是通过在找到标记末尾时添加零终止符 ('\0') 来修改您要在其中查找标记的字符串。所以迭代完之后,它就不再是内存中的连续字符串了。这就是为什么您不能从头开始并重新解析字符串中的标记的原因。

您可能有两种选择:

  • 首先复制字符串,然后对其进行标记

    char *p1;字符数组[100]; strcpy(array, "任何字符串都可以");
    
    字符tmp[100];
    strcpy(tmp,数组);
    p1 = strtok(tmp, " ");
    p1 = strtok(NULL, " ");
    
    strcpy(tmp,数组);
    p1 = strtok(tmp, " ");
    p1 = strtok(NULL, " ");
    
    printf("%c", p1);
    
  • 或者您可以在找到它们时保留指向单独标记的指针数组,然后重新使用数组,而不是重新调用 strtok()

strtok() actually modifies the string you are looking for tokens in by adding zero-terminator characters ('\0') to it when it finds the end of a token. So after iterating through it, it is no longer a continuous string in memory anymore. This is why you can not start again at the beginning and re-parse the string for tokens.

You probably have two choices:

  • make a copy of the string first and tokenise that instead

    char *p1; char array[100]; strcpy(array, "ANY STRING WOULD DO");
    
    char tmp[100];
    strcpy( tmp, array );
    p1 = strtok(tmp, " ");
    p1 = strtok(NULL, " ");
    
    strcpy( tmp, array );
    p1 = strtok(tmp, " ");
    p1 = strtok(NULL, " ");
    
    printf("%c", p1);
    
  • or you could keep an array of pointers to the separate tokens as you find them and re-use the array, rather than re-calling strtok().

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