[英]Reading binary data written using Python struct in C
I'm writing a binary file in Python to be read in C. The (MWE) code to write the file is:我正在用 Python 编写要在 C 中读取的二进制文件。编写文件的(MWE)代码是:
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:当我用 C 读取文件时,我得到乱码数据:
#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系统:Ubuntu 12.04,64位
Python: 2.7.3蟒蛇:2.7.3
In your C code, you read every item out one by one, which means you did not apply any alignment.在您的 C 代码中,您一项一项地读取每一项,这意味着您没有应用任何对齐方式。 Try this:
试试这个:
outfile.write(struct.pack('=didi', 1.2, 1, 1.3, 2))
hexdump test.bin hexdump test.bin
0000000 3333 3333 3333 3ff3 0001 0000 cccd cccc
0000010 cccc 3ff4 0002 0000
C code output: C代码输出:
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:如果你不改变python代码,仍然使用'didi',然后像这样改变c代码:
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.本次在 Fedora 17 上测试,使用 python 2.7.3,gcc 4.7.2,我更喜欢定义结构。
I should also say that it is possible to change the C program and not the python program to work.我还应该说,可以更改 C 程序而不是 python 程序来工作。 This is almost perfect except that on my system python writes a 28 byte file, and my C program expects 32 bytes.
这几乎是完美的,除了在我的系统上 python 写入一个 28 字节的文件,而我的 C 程序需要 32 字节。 It works without the assert.
它在没有断言的情况下工作。 Not ideal.
不理想。 Writing a junk int after testinttwo would fix the problem.
在 testinttwo 之后写一个垃圾 int 可以解决这个问题。
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
int main(int argc, char *argv[]) {
typedef struct { double testdouble;
int testint;
double testdoubletwo;
int testinttwo;
} teststruct;
teststruct abc;
FILE *f = fopen("test.bin", "rb");
assert(f);
/* assert(fread(&abc, sizeof(teststruct), 1, f));*/
printf("sizeof(teststruct) %d\n",sizeof(teststruct));
fread(&abc, sizeof(teststruct), 1, f);
fprintf(stderr, "testdouble: %f, testint: %d, testdouble: %f, testinttwo: %d", abc.testdouble, abc.testint, abc.testdoubletwo, abc.testinttwo);
fclose(f);
return 0;
}
Adjusted python program to fix the assert issue.调整了 python 程序以修复断言问题。
import struct
with open('test.bin', 'wb') as outfile:
outfile.write(struct.pack('didii', 1.2, 1, 1.3, 2,0))
C Output: C输出:
sizeof(teststruct) 32
testdouble: 1.200000, testint: 1, testdouble: 1.300000, testinttwo: 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.