Hi,
I have a problem reading floats that have been written to a binary file on a little endian machine (x86). When I try to read the file on a big endian machine (PPC), the floats get read in as 0.0000.
Integers get read in correctly and I just need to switch the endian to get the correct value. Here is the code that I'm using
float v[5];
int w[5];
FILE *fp = fopen("test.dat", "rb");
if (fp == NULL)
{
fp = fopen("test.dat", "wb");
v[0] = 1.2f;
v[1] = 2.3f;
v[2] = 3.4f;
v[3] = 4.5f;
v[4] = 5.6f;
w[0] = 10;
w[1] = 20;
w[2] = 30;
w[3] = 40;
w[4] = 50;
fwrite(v, sizeof(float), 5, fp);
fwrite(w, sizeof(int), 5, fp);
fclose(fp);
}
else
{
fp = fopen("test.dat", "rb");
fread(v, sizeof(float), 5, fp);
fread(w, sizeof(int), 5, fp);
printf("Got %f %f %f %f %f\n", v[0], v[1], v[2], v[3], v[4]);
printf("Got %d %d %d %d %d\n", w[0], w[1], w[2], w[3], w[4]);
}
exit(0);
The first if statement just writes the file if it doesn't exist. The output of the program when run on 68k or PPC is
Got -0.00000 0.00000 -0.00000 0.00000 0.00000
Got 167772160 335544320 503316480 671088640 838860800
Does anyone know what I can do to get the floats to read in correctly so that I can then swap the endian?
Thanks
Richard
Start Free Trial