求解释。fstream碰到十六进制1A为什么会这样

求解释。fstream遇到十六进制1A为什么会这样?
本帖最后由 canglong13 于 2013-08-01 00:15:08 编辑
在使用C++的fstream读取二进制文件时发现fstream遇到0x1A时(也就是十进制的26)就会将文件流状态标志为eof,然后fstream就无法读取后面的数据了。示例代码如下:

int _tmain(int argc, _TCHAR* argv[])
{
ofstream* testFile = new ofstream("B.dat");
if (!(*testFile))
{
cout<<"File is not open"<<endl;
system("pause");
return 0;
}

char c[sizeof(double)] = {1, 2, 26, 4, 5, 6, 7, 8};

for (int i = 0; i<sizeof(double); i++)
{
cout<<std::dec<<static_cast<int>(c[i])<<endl;
}

testFile->write(c, sizeof(double));

testFile->close();

cout<<"****************************"<<endl;

ifstream* iTestFile = new ifstream("B.dat");
if (!(*iTestFile))
{
cout<<"File is not open. iTestFile"<<endl;
system("pause");
return 0;
}

char cc[sizeof(double)];
iTestFile->read(cc, sizeof(double));

for (int i = 0; i < sizeof(double); i++)
{
cout<<std::dec<<static_cast<int>(cc[i])<<endl;
}

system("pause");
return 0;
}

不知道这是为什么?应该怎么来解决这个问题?
C++ IO fstream

------解决方案--------------------
0x1A在DOS/Windows系统中是文件结束符

new ofstream("B.dat", fstream::in 
------解决方案--------------------
 fstream::binary);
------解决方案--------------------

  EOF可以作为文本文件的结束标志,但不能作为二进制文件的结束符.
  feof函数既可以判断二进制文件,又可以判断文本文件.
  在windows下,以文本方式写入文件的\n会被转换为\r\n(也就是0x0D0A),
  而在读入的时候,\r\n会被转换回\n,
  还以内码为26(0x1A)的字符也作为文件结束符。