问题
I need to preserve the exact binary representation of some doubles in a text file with other ascii values, so I am using "%a" as suggested in this question.
fprintf (pFile, "Scale: %a, %a, %a\n", scale.x, scale.y, scale.z);
However, when I try to read it in with "%la" the scanf returns 0 items read.
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!
When I open up the debugger, I see that the string buffer is exactly as I expected it.
buf ... "Scale: 0x1.fc70e3p-1, 0x1.fc70e3p-1, 0x1.fc70e3p-1\n" ... char[1000]
So why can't it read it?
As requested by Nate Eldredge, here is my MCVE version:
#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!
}
Note: I am using MS Visual Studio 2013
Second Note: I need to send the source code and data files to a third party, who has his own compiler. So the save format has to be relatively platform-independent.
回答1:
Microsoft VS2013 strtod
, sscanf
, and istringstream
, etc. did not implement the c99 c++11 standard and they cannot parse hex floats or hex doubles.
Related Bug Report:
https://github.com/JuliaLang/julia/issues/6349
来源:https://stackoverflow.com/questions/55930726/sscanf-fails-to-read-double-hex