Reading binary data written using Python struct in C

Johannes Bauer

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

PasteBT

In your C code, you read every item out one by one, which means you did not apply any alignment. Try this:

outfile.write(struct.pack('=didi', 1.2, 1, 1.3, 2))

hexdump test.bin

0000000 3333 3333 3333 3ff3 0001 0000 cccd cccc
0000010 cccc 3ff4 0002 0000                    

C code output:

testdouble: 1.200000, testint: 1, testdouble: 1.300000, testinttwo: 2

If you not change python code, still use 'didi', then change c code like this:

struct D {
    double td;
    int ti;
    double td2;
   int ti2;
};


struct D d;
fread(&d, sizeof(struct D), 1, f);
fprintf(stderr, "testdouble: %f, testint: %d, testdouble: %f, testinttwo: %d", d.td, d.ti, d.td2, d.ti2);

This test on Fedora 17, using python 2.7.3, gcc 4.7.2, I prefer define the structure.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ reading binary data to struct

From Dev

Reading Binary Data Using Python

From Dev

Reading binary data into struct with ifstream

From Dev

Reading binary data into struct with ifstream

From Dev

Fstream not reading a complete struct from binary data (C++)

From Dev

Reading binary data in python (to replace C code)

From Dev

Python Struct for Binary Data

From Dev

C - Reading from binary file into any struct

From Dev

Reading string into struct data in C

From Dev

Reading a Binary File that was generated with C++ data types Using Numpy

From Dev

Reading data from file to struct using fread

From Dev

Reading in binary file and using data correctly

From Dev

Reading data from file into a struct (C)

From Dev

Reading struct data from memory in C?

From Dev

Reading a raw binary data in python and converting it into ascii

From Dev

Reading binary data into std::string c++

From Dev

Reading a struct in binary with ifstream read()

From Dev

Reading data from a text file, written in a specific period of time in Python

From Dev

Reading binary data issues

From Dev

Reading binary data in Java

From Dev

Binary data reading

From Dev

Reading in Fortran binaries written with Python

From Dev

reading binary data from tcp clients using netty

From Dev

Reading binary files c

From Dev

Reading binary PGM on C

From Dev

Non-contiguous data reading of binary file in python

From Dev

Writing/reading large vectors of data to binary file in c++

From Dev

c++ reading integers from binary file, missing some data

From Dev

reading individual bytes of multiple binary files using the Python module fileinput

Related Related

HotTag

Archive