c - 当我使用多个字符串时函数 sscanf_s 失败

标签 c visual-studio-2010 parsing scanf

我不明白为什么下面的代码会失败:

char    toto[54], titi[54]; //I already tried with allocations

sscanf_s(line, "%s-%s", &toto, &titi, sizeof(toto) + sizeof(titi));

sscanf_s(line, "%s-%s", &toto, &titi, sizeof(toto) + sizeof(titi));

我的问题只是字符串(float、int、double 等。没问题),我使用的是 Visual 2010。

有人有想法吗? 非常感谢您的回答。

最佳答案

来自sscanf_s()引用页:

The sscanf_s function reads data from buffer into the location given by each argument. The arguments after the format string specify pointers to variables with a type that corresponds to a type specifier in format. Unlike the less secure version sscanf, a buffer size parameter is required when using the type field characters c, C, s, S and [. The buffer size in characters must be supplied as an additional parameter after each buffer which requires it. For more information, see scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l and scanf Type Field Characters.

意思是每个缓冲区后面必须跟着它的大小:

sscanf_s(line, "%s-%s", toto, sizeof(toto), titi, sizeof(titi));

此外,- 不是空白字符,不会作为 "%s" 格式说明符的终止符,因此如果 line 包含 hello-world 然后它会被读入 tototiti 不会被赋值。要将 - 用作终止符,请使用扫描集:

if (2 == sscanf_s(line, "%[^-]-%s", toto, sizeof(toto), titi, sizeof(titi)))
{
    /* Both 'toto' and 'titi' where assigned. */
}

关于c - 当我使用多个字符串时函数 sscanf_s 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13279209/

相关文章:

c# - Crystal 报表在 Visual Studio 2010 中不工作

html - 如何在 Visual Studio 中强制使用特定版本的 CSS?

javascript - 如何激活 VS2010 的 JavaScript 进程调试管理器?

javascript - 如何在 jQuery 中解析 JSON 响应?

parsing - 用Pig解析系统日志文件

c - 借助双指针指向第一个数组元素不起作用

c - C语言中如何判断用户输入的数据是否为 float ?

c - 从 C 中的子域中提取域

android - 解析器正在读取不在 XML 文件中的标签

c - 'Node' 之前的预期表达式