2

我需要在具有其他 ascii 值的文本文件中保留某些双精度的确切二进制表示,因此我按照问题中的建议使用“%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

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

4

1 回答 1

1

Microsoft VS2013 strtodsscanfistringstream等没有实现c99 c++11 标准,它们无法解析十六进制浮点数或十六进制双精度数。

于 2019-05-01T16:43:48.123 回答