简体   繁体   English

文件读取问题

[英]File reading problem

I have this binary file with showing the correct value when I opened the file using HexView. 当我使用HexView打开文件时,我具有显示正确值的二进制文件。

4c 60 02 aa b4 c2 d1 e3 1a 01 00 00 8c 01 00 00 f5 01 00 00 52 02 00 00 bd 02 00 00 20 03 00 00 32 03 00 00 59 03 00 00 4c 60 02 aa b4 c2 d1 e3 1a 01 00 00 8c 01 00 00 f5 01 00 00 52 02 00 00 bd 02 00 00 20 03 00 00 32 03 00 00 59 03 00 00

When I uses fread to read the 40 bytes data into a char buffer, it failed. 当我使用fread将40个字节的数据读入char缓冲区时,它失败了。 From 9th byte data onwards, all the read back data is 0x00. 从第9个字节数据开始,所有回读数据均为0x00。

int main()
{
    FILE *stream;
    char flag[40]={0};
    size_t numread = 0;
    UINT theme = 0;

    if ((stream = fopen("alignment.bin", "r")) != NULL)
    {
        numread = fread(&flag, 1, 40, stream);

        fclose(stream);
    }
    else
    {
        cout << "File open failed" << endl;
    }
    system ("pause");
    return 0;
}

Try using "rb" instead of "r" . 尝试使用"rb"代替"r" There might be some weird text formatting issues. 可能存在一些奇怪的文本格式问题。

Specifying the b makes it read in pure binary with no formatting. 指定b使它以纯二进制格式读取,不带格式。

0x1A == 26 == ctrl-Z == EOF. 0x1A == 26 == ctrl-Z == EOF

If you read in text mode, the stream consider the flow finished after that point (what follow is "rubbish for other transmissions"). 如果您以文本模式阅读,则流将认为该点之后已完成流(接下来的内容是“其他传输的垃圾”)。

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

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