简体   繁体   English

ifstream读取二进制数据发出0x00字节

[英]ifstream read binary data issue 0x00 byte

Hi everyone i have an issue while reading binary data from a binary file as following: 大家好我从二进制文件读取二进制数据时遇到问题如下:

File Content: D3 EE EE 00 00 01 D7 C4 D9 40 文件内容:D3 EE EE 00 00 01 D7 C4 D9 40

char * afpContentBlock = new char[10];
ifstream inputStream(sInputFile, ios::in|ios::binary);

if (inputStream.is_open()))
{
    inputStream.read(afpContentBlock, 10);

    int n = sizeof(afpContentBlock)/sizeof(afpContentBlock[0]); // Print 4

    // Here i would like to check every byte, but no matter how i convert the 
    // char[] afpContentBlock, it always cut at first byte 0x00.
}

I know this happens cause of the byte 0x00. 我知道这发生在字节0x00的原因。 Is there a way to manage it somehow ? 有办法以某种方式管理它吗? I have tried to write it with an ofstream object, and it works fine since it writes out the whole 10 bytes. 我试图用ofstream对象编写它,它工作正常,因为它写出了整个10个字节。 Anyway i would like to loop through the whole byte array to check bytes value. 无论如何,我想遍历整个字节数组来检查字节值。

Thank you very much. 非常感谢你。

It's much easier to just get how many bytes you read from the ifstream like so: 从ifstream读取多少字节要容易得多:

if (inputStream.is_open()))
{
   inputStream.read(afpContentBlock, 10);
   int bytesRead = (int)inputStream.gcount();

   for( int i = 0; i < bytesRead; i++ )
   {
      // check each byte however you want
      // access with afpContentBlock[i]
   }
}

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

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