读书人

ifstream close出错解决方案

发布时间: 2012-03-18 13:55:39 作者: rapoo

ifstream close出错
为什么这样写,会出现错误?????????
ifstream file1;
file1.open("D:\\60x55.BMP", ios_base::binary | ios_base::in);
if (!file1)
{
cout<<"error";
}

file1.seekg( 0, ios::end );
fstream::pos_type flen = file1.tellg();
file1.seekg( 0, ios::beg );

char *buf = new char(flen);
file1.read(buf, flen);
file1.close();

编译连接没问题, 但运行的时候, 会出现一个对话框,This may be due to a corruption of the heap, and indicates a bug in filectrl_test.exe or any of the DLLs it has loaded.

如果将char *buf = new char(flen);
file1.read(buf, flen);
去掉的话, 就没事,能运行。

[解决办法]
char *buf = new char[flen];

读书人网 >C++

热点推荐