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.
buf
? – Evansfgets
stops at EOL or\0
So it's dependent on the data stored go read correctly. – Elitism0.993049, 0.993049, 0.993049
which seems to be right. – Evans%la
is allowed... – Evans%la
to%lf
, what happens? I think the standard says that all ofaeEfFgG
are supposed to behave identically forscanf
, including the handling of hex floats (en.cppreference.com/w/c/io/fscanf). The MS documentation I linked above doesn't mention handling hex floats withf
, but maybe it will work anyway... – Evansstrtod
in 2013. Now what? Write it myself? – Bartleunion
is the way provided by the C standard to avoid the strict aliasing violation. You generally want to avoid a direct cast. – Coworker