文件阅读问题

okw*_*oei 1 c++ file

当我使用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

当我使用fread将40字节数据读入char缓冲区时,它失败了.从第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;
}
Run Code Online (Sandbox Code Playgroud)

Mys*_*ial 9

尝试使用"rb"而不是"r".可能存在一些奇怪的文本格式问题.

指定b使其以纯二进制形式读取而不进行格式化.