大家好我从二进制文件读取二进制数据时遇到问题如下:
文件内容: 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个字节。无论如何,我想遍历整个字节数组来检查字节值。
非常感谢。
答案 0 :(得分: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]
}
}