Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何正确关闭BinaryReader类,以便再次打开同一文件?_C++_.net_Visual Studio 2010_Io - Fatal编程技术网

C++ 如何正确关闭BinaryReader类,以便再次打开同一文件?

C++ 如何正确关闭BinaryReader类,以便再次打开同一文件?,c++,.net,visual-studio-2010,io,C++,.net,Visual Studio 2010,Io,使用VC++2010、.NET3.5 我的应用程序在第二次迭代时崩溃,而在这条线上循环 System::IO::BinaryReader^ binReader = ... 它可以很好地编译,但是运行时,我得到了“.stopped working”对话框(在第二次迭代中,我通过在循环的开头放一个::MessageBox()进行了检查) 如何正确关闭BinaryReader类,以便再次打开同一文件?问题可能在于“读取文件,执行操作”代码。如果你把它注释掉,它还会崩溃吗?维尔德,我认为删除(忘记关

使用VC++2010、.NET3.5

我的应用程序在第二次迭代时崩溃,而在这条线上循环

System::IO::BinaryReader^ binReader = ...

它可以很好地编译,但是运行时,我得到了“.stopped working”对话框(在第二次迭代中,我通过在循环的开头放一个::MessageBox()进行了检查)


如何正确关闭BinaryReader类,以便再次打开同一文件?

问题可能在于“读取文件,执行操作”代码。如果你把它注释掉,它还会崩溃吗?维尔德,我认为
删除
(忘记关闭)就足够了。如果删除
gcnew
delete
,它是否遵循堆栈语义并正常工作?(无论如何,从异常安全的角度来看,这实际上更好)。有一点可能不是你的问题,那就是使用调试器(而不是消息框)找出异常是什么。
System::String^ fileName = GetFileName();
bool continue = true;
do {
    System::IO::BinaryReader^ binReader = gcnew System::IO::BinaryReader(System::IO::File::Open(fileName, System::IO::FileMode::Open));
    //
    // read file,
    // do things,
    //
    binReader.close();
    delete binReader;
} while (continue);