【发布时间】:2022-01-09 07:04:42
【问题描述】:
我将以下内容存储在 char 数组中
"1, 1.0, 1.000, 1.0000"
我正在尝试将其解析为一个 int 和三个双精度数
sscanf(myString, "%d %lf %lf %lf", &(myStruct->I1), &(myStruct->D1), &(myStruct->D2), &(myStruct->D3);
printf("%d %lf %lf %lf", myStruct->I1, myStruct->D1, myStruct->D2, myStruct->D3);
输出
1 0.000000 0.000000 0.000000
【问题讨论】:
-
把它变成minimal reproducible example。应该不难
-
什么会消耗逗号?
-
始终检查
scanf及其亲属的返回值。 -
sscanf(myString, "%d ,%lf ,%lf ,%lf", ... )。这样做会捕获逗号之前的任何空格,而逗号之后的任何空格都会被%lf说明符自动捕获。 -
好的,谢谢!我错过了逗号!
标签: c struct scanf c-strings conversion-specifier