将二进制数据(从文件)读取到结构中 [英] Read binary data (from file) into a struct

查看:95
本文介绍了将二进制数据(从文件)读取到结构中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从文件(尤其是从zip文件)读取二进制数据。 (要了解有关zip格式结构的更多信息,请参见 http://en.wikipedia.org/wiki / ZIP_%28file_format%29

I'm reading binary data from a file, specifically from a zip file. (To know more about the zip format structure see http://en.wikipedia.org/wiki/ZIP_%28file_format%29)

我创建了一个存储数据的结构:

I've created a struct that stores the data:

typedef struct {
                                            /*Start Size            Description                                 */
    int signatute;                          /*   0  4   Local file header signature = 0x04034b50                */
    short int version;                      /*   4  2   Version needed to extract (minimum)                     */
    short int bit_flag;                     /*   6  2   General purpose bit flag                                */
    short int compression_method;           /*   8  2   Compression method                                      */
    short int time;                         /*  10  2   File last modification time                             */
    short int date;                         /*  12  2   File last modification date                             */
    int crc;                                /*  14  4   CRC-32                                                  */
    int compressed_size;                    /*  18  4   Compressed size                                         */
    int uncompressed_size;                  /*  22  4   Uncompressed size                                       */
    short int name_length;                  /*  26  2   File name length (n)                                    */
    short int extra_field_length;           /*  28  2   Extra field length (m)                                  */
    char *name;                             /*  30  n   File name                                               */
    char *extra_field;                      /*30+n  m   Extra field                                             */

} ZIP_local_file_header;

sizeof(ZIP_local_file_header)返回的大小是40,但是如果使用 sizeof 运算符计算每个字段的总和,则总大小为38。

The size returned by sizeof(ZIP_local_file_header) is 40, but if the sum of each field is calculated with sizeof operator the total size is 38.

如果我们有下一个结构:

If we have the next struct:

typedef struct {
    short int x;
    int y;
} FOO;

sizeof(FOO)返回8,因为每次分配4个字节的内存。因此,要分配 x 保留4个字节(但实际大小为2个字节)。如果我们需要另一个 short int ,它将填充先前分配的其余2个字节。但是,由于我们有一个 int ,它将被分配加上4个字节,并且浪费了2个字节。

sizeof(FOO) returns 8 because the memory is allocated with 4 bytes every time. So, to allocate x are reserved 4 bytes (but the real size is 2 bytes). If we need another short int it will fill the resting 2 bytes of the previous allocation. But as we have an int it will be allocated plus 4 bytes and the empty 2 bytes are wasted.

To从文件中读取数据,我们可以使用函数 fread

To read data from file, we can use the function fread:

ZIP_local_file_header p;
fread(&p,sizeof(ZIP_local_file_header),1,file);

但是由于中间有空字节,因此无法正确读取。

But as there're empty bytes in the middle, it isn't read correctly.

如何使用 ZIP_local_file_header 不浪费字节来顺序有效地存储数据?

What can I do to sequentially and efficiently store data with ZIP_local_file_header wasting no bytes?

推荐答案

C struct s只是将相关数据分组在一起,它们不指定内存中的特定布局。 (就像未定义 int 的宽度一样。)Little-endian / Big-endian也未定义,并且取决于处理器。

C structs are just about grouping related pieces of data together, they do not specify a particular layout in memory. (Just as the width of an int isn't defined either.) Little-endian/Big-endian is also not defined, and depends on the processor.

不同的编译器,在不同体系结构或操作系统上的同一编译器等,所有布局结构都将不同。

Different compilers, the same compiler on different architectures or operating systems, etc., will all layout structs differently.

要读取的文件格式是根据字节的位置来定义的,尽管结构看起来非常方便和诱人,但它不是正确的解决方案。您需要将文件视为 char [] 并取出所需的字节并进行移位,以使数字由多个字节等组成。

As the file format you want to read is defined in terms of which bytes go where, a struct, although it looks very convenient and tempting, isn't the right solution. You need to treat the file as a char[] and pull out the bytes you need and shift them in order to make numbers composed of multiple bytes, etc.

这篇关于将二进制数据(从文件)读取到结构中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆