我需要在带有其他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”...... 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标准,它们无法解析hex浮点数或hex hex。