sscanf 无法读取双十六进制

sscanf fails to read double hex

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

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

但是,当我尝试用“%la”读取它时,scanf returns 读取了 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 ... "Scale: 0x1.fc70e3p-1, 0x1.fc70e3p-1, 0x1.fc70e3p-1\n" ... char[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

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

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