C++ 调用stream.close不会';不要发布文件
执行如下代码后:C++ 调用stream.close不会';不要发布文件,c++,file,ofstream,C++,File,Ofstream,执行如下代码后: { ofstream ofs("file.txt", ios::app); ofs << "Output" << endl; ofs.close(); Sleep(100); //just in case bool opened = ofs.is_open(); //always false } //ofs out of scope { ofs流(“file.txt”,ios::app); ofsclose正在
{
ofstream ofs("file.txt", ios::app);
ofs << "Output" << endl;
ofs.close();
Sleep(100); //just in case
bool opened = ofs.is_open(); //always false
} //ofs out of scope
{
ofs流(“file.txt”,ios::app);
ofsclose
正在做正确的事情,ofs
不再打开…使用Process Explorer(Windows,lsof
在Linux中)检查进程是否正在使用该文件。您的ofs
对象作用域是全局的吗?如果不是,当ofs
超出作用域时,文件句柄会发生什么情况?@txtechhelp其本地文件,甚至在超出作用域后仍然没有释放dofstream::close内部调用filebuf::close,在出现故障时设置failbit。因此您可以d通过调用.rdstate()检查故障位。引用:和