c++ - sscanf 无法读取双十六进制

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

我需要在具有其他 ascii 值的文本文件中保留某些 double 值的精确二进制表示,因此我按照 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 ... "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 标准,它们不能解析十六进制 float 或十六进制 double 。

关于c++ - sscanf 无法读取双十六进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55930726/

相关文章:

c++ - 如何在 C++ Qt 中填充静态 QMap 的值?

c - 如何使用 winapi 获取 YYYYMMDD 格式的日期

c - C 中的 Scanf 导致奇怪的控制台行为?

python - freeopcua c++客户端和python opcua的组合在getChild()上引发错误

c# - 实现并行异常以获得错误树?

c - 如何用 goto 翻译 if .. else 语句?

c - 当您将 12ab 等输入到 scanf ("%d",&argu) 时会发生什么?

C语言-scanf更多输入但不知道有多少输入

c++ - 如何确保迭代器模板参数与模板类的模板参数具有相同的数据类型

c - list 文件中的 requestExecutionLevel 和 CopyFile() 函数失败