0

大家好,我在从二进制文件中读取二进制数据时遇到问题,如下所示:

文件内容: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.
}

我知道这是字节 0x00 的原因。有没有办法以某种方式管理它?我试图用一个 ofstream 对象来写它,它工作得很好,因为它写出了整个 10 个字节。无论如何,我想遍历整个字节数组来检查字节值。

非常感谢你。

4

1 回答 1

2

像这样获取从 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]
   }
}
于 2012-08-08T11:06:12.947 回答