strtok() 获取字符串的第一个标记时出现问题
以下代码无法获取字符串的第一个标记:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
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 callstrtok(array, "");
,array
now looks likeANY'\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, " ");
returnsNULL
. 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.请查看此处给出的示例: MSDN: strtok , wcstok, _mbstok
Please take a look at the example given here: MSDN: strtok, wcstok, _mbstok
这可行:
strtok 修改它传递的字符串,因此如果您想重新解析它,您需要再次复制它。
如果您只想保留令牌,只需复制指针即可。
This would work:
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.
请记住,
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 returnNULL
, and when you then try to print p1, it'll try to dereferenceNULL
, and probably fail.Note btw, that you need to use
"%s"
instead of"%c"
, because you're printing a string, not a character.strtok()
实际上是通过在找到标记末尾时添加零终止符 ('\0') 来修改您要在其中查找标记的字符串。所以迭代完之后,它就不再是内存中的连续字符串了。这就是为什么您不能从头开始并重新解析字符串中的标记的原因。您可能有两种选择:
首先复制字符串,然后对其进行标记
或者您可以在找到它们时保留指向单独标记的指针数组,然后重新使用数组,而不是重新调用
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
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()
.