简体   繁体   English

读取二进制文件的问题

[英]Problems reading binary file

I'm trying to read the first 6 bytes of a file, but it's giving me weird results, and I can't seem to figure out what I'm doing wrong.我正在尝试读取文件的前 6 个字节,但它给了我奇怪的结果,而且我似乎无法弄清楚我做错了什么。

My code:我的代码:

struct Block {
    char fileSize[3];
    char initialDataBlockId[3];
};

int main(int c, char **a) {

    ifstream file("C\\main_file_cache.idx0", ios::binary);

    Block block;

    file.get((char*)&block, sizeof(block));

    printf("File Size: %i\n", block.fileSize);
    printf("Initial Data Block ID: %i\n", block.initialDataBlockId);

    file.close();

    system("pause");
    return 0;
}

Before I ran the code, I opened the file in a binary editor, and it showed me this hex code:在运行代码之前,我在二进制编辑器中打开文件,它显示了这个十六进制代码:

    00 00 00 00-00 00 05 af-4b 00 00 01-26 df cd 00
    00 6f 03 3f-ed 00 03 61-05 08 35 00-04 8b 01 61
    59 00 08 39-03 23 0a 00-05 6c 00 35-d0 00 06 fe
    03 69 d8 00-07 19

There are a total of 54 bytes.一共有54个字节。 The first 6 bytes are just zero.前 6 个字节仅为零。

So, I expected my program to produce the following output:所以,我希望我的程序产生以下输出:

File Size: 0
Initial Data Block ID: 0

Instead, the outputs is as follows:相反,输出如下:

File Size: 10419128
Initial Data Block ID: 10419131

This result makes no sense.这个结果没有意义。 Maybe there is something wrong with my code?也许我的代码有问题?

You should use type unsigned char in your Block structure.您应该在Block结构中使用unsigned char类型。

You should use file.read() to read binary data instead of file.get() .您应该使用file.read() file.get()读取二进制数据。

You are printing the addresses of the arrays in the Block structure, not their contents, furthermore the specifier %i expects an int , not a char * , so the behavior in undefined and you get some weird integer value but anything culd have happened, including program termination.您正在Block结构中打印数组的地址,而不是它们的内容,此外说明符%i需要一个int而不是char * ,因此未定义的行为并且您得到一些奇怪的整数值但可能发生的任何事情,包括程序终止。 Increasing the warning level is advisable so the compiler warns about such silly mistakes.建议提高警告级别,以便编译器警告此类愚蠢的错误。

If the file format is little endian, you could convert these 3 byte arrays to numbers this way:如果文件格式是小端格式,您可以通过这种方式将这 3 个字节的数组转换为数字:

int block_fileSize =  (unsigned char)block.fileSize[0] + 
                     ((unsigned char)block.fileSize[1] << 8) + 
                     ((unsigned char)block.fileSize[2] << 16);
int block_initialDataBlockId =  (unsigned char)block.initialDataBlockId[0] +
                               ((unsigned char)block.initialDataBlockId[1] << 8) +
                               ((unsigned char)block.initialDataBlockId[2] << 16);
printf("File Size: %i\n", block_fileSize);
printf("Initial Data Block ID: %i\n", block_initialDataBlockId);

If you want to read a binary data you can use a read method from ifstream and also write method from ofstream .如果你想读取二进制数据,你可以使用ifstreamread方法和ofstreamwrite方法。

istream & ifstream::read (char * s, streamsize n);
ostream & ofstream::write (const char * s, streamsize n);

You have to know that binary mode is useless for UNIX systems and text mode is only useful.你要知道二进制模式对UNIX系统没用,文本模式才有用。

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

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