我什至在阅读文档后也不确定如何使用sscanf做到这一点。
这是我想做的:
给定一串文字:
最多读取前64个字符或直到达到空格为止
然后将有一个空格,一个=,然后是另一个空格。
接下来,我想提取另一个字符串,直到该字符串的末尾或是否达到8192个字符。我还希望将第二个字符串“\ n”中的所有匹配项更改为实际的换行符。
我有:“%64s =%8192s”,但我认为这是不正确的。
谢谢
例如:
element.name =你好\ nworld
字符串1的element.name和string2为
hello
world
最佳答案
我确实为此推荐std::regex,但是除此之外,您应该可以进行一些错误检查:
#include <cstdio>
int main(int argc, const char *argv[])
{
char s1[65];
char s2[8193];
if (2!=std::scanf("%64s = %8192s", s1, s2))
puts("oops");
else
std::printf("s1 = '%s', s2 = '%s'\n", s1, s2);
return 0;
}
关于c++ - sscanf用于这种类型的字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8685769/