#include <stdio.h>
struct mystruct
{
int i;
char cha;
};
int main(void)
{
FILE *stream;
struct mystruct s;
stream = fopen("TEST.$$$", "wb"))
s.i = 0;
s.cha = 'A';
fwrite(&s, sizeof(s), 1, stream);
fclose(stream);
return 0;
}
但是如何在go或python中将一个结构wirte到文件中呢?我希望结构中的数据是连续的。在
在Python中,您可以使用
^{1}$ctypes
模块,该模块允许您生成布局与C类似的结构,并将其转换为字节数组:Python中有一种最简单的方法,使用
^{pr2}$struct.pack
并手动提供布局作为第一个参数('ic'
表示int
,后跟一个字符):Go可以通过
encoding/binary
对结构进行编码注意:您将使用不同的结构对齐,填充和endianness,因此,如果您想构建真正可互操作的程序,请使用特殊格式,如Google Protobuf
相关问题 更多 >
编程相关推荐