gpt4 book ai didi

c - Strtok 及其输入

转载 作者:行者123 更新时间:2023-11-30 16:43:17 27 4
gpt4 key购买 nike

我在 while 循环中使用 Strtok 将输入分成三个字符串,例如:
input="Command path 'you are beautiful'"拆分为:

tok1="Command"<br/>
tok2="path"<br/>
tok3="'you are beautiful'"

我不能连续使用 strtok 三次,因为 tok3 只是“你”。
我的问题是,当我使用 strtok 时,初始变量输入会发生什么?
在第一次调用 strtok 后,我希望输入为“路径 'you are beautiful'”,然后在第二次调用后输入“'you are beautiful”,因此在运行 strtok 时逐渐减少我的初始字符串。
有可能吗?如果没有,我该怎么做?

最佳答案

Strtok 行为在标准 ( http://pubs.opengroup.org/onlinepubs/009695399/functions/strtok.html ) 中定义如下:

A sequence of calls to strtok() breaks the string pointed to by s1 into a sequence of tokens, each of which is delimited by a byte from the string pointed to by s2. The first call in the sequence has s1 as its first argument, and is followed by calls with a null pointer as their first argument. The separator string pointed to by s2 may be different from call to call.

The first call in the sequence searches the string pointed to by s1 for the first byte that is not contained in the current separator string pointed to by s2. If no such byte is found, then there are no tokens in the string pointed to by s1 and strtok() shall return a null pointer. If such a byte is found, it is the start of the first token.

The strtok() function then searches from there for a byte that is contained in the current separator string. If no such byte is found, the current token extends to the end of the string pointed to by s1, and subsequent searches for a token shall return a null pointer. If such a byte is found, it is overwritten by a null byte, which terminates the current token. The strtok() function saves a pointer to the following byte, from which the next search for a token shall start.

Each subsequent call, with a null pointer as the value of the first argument, starts searching from the saved pointer and behaves as described above.

这意味着您只需调用 strtok 两次,然后确定第二个子字符串的\0 后面的位置即可获得您想要的第三部分。

但是,这似乎不是一个合理的方法。它在处理错误(如第三个子字符串为空时)和 future 潜在的扩展方面都很不灵活。此外,由于strtok接口(interface)的设计,使用它根本不是线程安全的。

手动编写一个小型词法分析器/解析器来执行您想要的操作可能是一个更好的主意,或者使用专门为构建词法分析器(以及解析器,如果需要)而设计的工具。我个人在使用 Flex 方面获得了很好的经验,但还有其他选择。

关于c - Strtok 及其输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45378130/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com