use*_*743 0 c parsing scanf visual-studio-2010
我不明白为什么下面的代码失败了:
char toto[54], titi[54]; //I already tried with allocations
sscanf_s(line, "%s-%s", &toto, &titi, sizeof(toto) + sizeof(titi));
Run Code Online (Sandbox Code Playgroud)
要么
sscanf_s(line, "%s-%s", &toto, &titi, sizeof(toto) + sizeof(titi));
Run Code Online (Sandbox Code Playgroud)
我的问题只有字符串(float,int,double等等),我使用Visual 2010.
有没有人有想法?非常感谢您的回答.
从sscanf_s()参考页面:
sscanf_s函数将数据从缓冲区读入每个参数给定的位置.格式字符串后面的参数指定指向变量的指针,其类型与格式中的类型说明符相对应.与安全性较低的版本sscanf不同,使用类型字段字符c,C,s,S和[时,需要一个缓冲区大小参数.在需要它的每个缓冲区之后,必须以字符形式提供缓冲区大小作为附加参数.有关更多信息,请参阅scanf_s,_scanf_s_l,wscanf_s,_wscanf_s_l和scanf类型字段字符.
意味着每个缓冲区必须跟随其大小:
sscanf_s(line, "%s-%s", toto, sizeof(toto), titi, sizeof(titi));
Run Code Online (Sandbox Code Playgroud)
另外,-不是空格字符不会作为"%s"格式说明符的终止符,所以如果line包含hello-world它,那么它将被读入toto并且titi不会被分配.要-用作终结符,请使用扫描集:
if (2 == sscanf_s(line, "%[^-]-%s", toto, sizeof(toto), titi, sizeof(titi)))
{
/* Both 'toto' and 'titi' where assigned. */
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4954 次 |
| 最近记录: |