【问题标题】:sscanf fails to read double hexsscanf 无法读取双十六进制
【发布时间】:2019-09-19 16:38:30
【问题描述】:

我需要在具有其他 ascii 值的文本文件中保留某些双精度值的精确二进制表示,因此我按照this 问题中的建议使用“%a”。

fprintf (pFile, "Scale: %a, %a, %a\n", scale.x, scale.y, scale.z);

但是,当我尝试使用“%la”读取它时,scanf 返回已读取的 0 个项目。

double x=0, y=0, z=0;
fgets(buf, sizeof buf, pFile);
int test = sscanf (buf, "Scale: %la, %la, %la\n", &x, &y, &z);
// test is zero!

当我打开调试器时,我看到字符串缓冲区完全符合我的预期。

buf ... "比例:0x1.fc70e3p-1, 0x1.fc70e3p-1, 0x1.fc70e3p-1\n" ... 字符[1000]

那为什么它不能读呢?

根据Nate Eldredge 的要求,这是我的 MCVE 版本:

#include <stdio.h>
int main(int argc, char *argv[])
{
    double x=0, y=0, z=0;
    const char* buf = "Scale: 0x1.fc70e3p-1, 0x1.fc70e3p-1, 0x1.fc70e3p-1\n";
    int test = sscanf(buf, "Scale: %la , %la , %la", &x, &y, &z);
    // test is zero!
}

注意:我使用的是 MS Visual Studio 2013

第二个注意:我需要将源代码和数据文件发送给第三方,第三方有自己的编译器。所以保存格式必须相对独立于平台。

【问题讨论】:

标签: c++ c scanf visual-c++-2013


【解决方案1】:

Microsoft VS2013 strtodsscanfistringstream没有实现了 c99 c++11 标准,它们不能解析十六进制浮点数或十六进制双打。

【讨论】:

    猜你喜欢
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 2023-03-30
    • 1970-01-01
    • 2013-11-20
    • 2016-09-24
    • 1970-01-01
    相关资源
    最近更新 更多