我需要在具有其他 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 strtod
、sscanf
、istringstream
等未实现c99 c++ 11 标准,它们不能解析十六进制 float 或十六进制 double 。
关于c++ - sscanf 无法读取双十六进制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55930726/