I'm writing a binary file in Python to be read in C. The (MWE) code to write the file is:
import struct
with open('test.bin', 'wb') as outfile:
outfile.write(struct.pack('didi', 1.2, 1, 1.3, 2))
When I read the file in C, I get garbled data:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main(int argc, char *argv[]) {
double testdouble, testdoubletwo;
int testint, testinttwo;
FILE *f = fopen("test.bin", "rb");
assert(f);
assert(fread(&testdouble, sizeof(testdouble), 1, f));
assert(fread(&testint, sizeof(testint), 1, f));
assert(fread(&testdoubletwo, sizeof(testdoubletwo), 1, f));
assert(fread(&testinttwo, sizeof(testinttwo), 1, f));
fprintf(stderr, "testdouble: %f, testint: %d, testdouble: %f, testinttwo: %d", testdouble, testint, testdoubletwo, testinttwo);
return 0;
}
Output:
testdouble: 1.200000, testint: 1, testdouble: -92559641157289301412905710012271939667257667601819249288413184.000000, testinttwo: 1073007820
If I leave out the integers, it works for this small example, but not for my actual problem where I'm reading a few dozen doubles. Some of them (not the first, not the last) end up garbled.
System: Ubuntu 12.04, 64bit
Python: 2.7.3