繁体   English   中英

读取在 C 中使用 Python struct 编写的二进制数据

[英]Reading binary data written using Python struct in C

我正在用 Python 编写要在 C 中读取的二进制文件。编写文件的(MWE)代码是:

import struct

with open('test.bin', 'wb') as outfile:  
    outfile.write(struct.pack('didi', 1.2, 1, 1.3, 2))

当我用 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;
}

输出:

testdouble: 1.200000, testint: 1, testdouble: -92559641157289301412905710012271939667257667601819249288413184.000000, testinttwo: 1073007820

如果我省略了整数,它适用于这个小例子,但不适用于我正在阅读几十个双打的实际问题。 其中一些(不是第一个,也不是最后一个)最终出现乱码。

系统:Ubuntu 12.04,64位
蟒蛇:2.7.3

在您的 C 代码中,您一项一项地读取每一项,这意味着您没有应用任何对齐方式。 试试这个:

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代码输出:

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

如果你不改变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);

本次在 Fedora 17 上测试,使用 python 2.7.3,gcc 4.7.2,我更喜欢定义结构。

我还应该说,可以更改 C 程序而不是 python 程序来工作。 这几乎是完美的,除了在我的系统上 python 写入一个 28 字节的文件,而我的 C 程序需要 32 字节。 它在没有断言的情况下工作。 不理想。 在 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;
}

调整了 python 程序以修复断言问题。

import struct

with open('test.bin', 'wb') as outfile:
    outfile.write(struct.pack('didii', 1.2, 1, 1.3, 2,0))

C输出:

sizeof(teststruct) 32
testdouble: 1.200000, testint: 1, testdouble: 1.300000, testinttwo: 2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM