我正在使用 fstream 打开文件进行写入。我不想覆盖现有文件,因此经过一番搜索后,我发现了 ios::noreplace。但是当我编译这个时:
#include <fstream>
using namespace std;
//......Did something else.
ofstream fout;
fout.open(outputFile,ios::noreplace);//outputFile is a C string
我收到一个错误
error: ‘noreplace’ is not a member of ‘std::ios’
我只是想知道 ios::noreplace 是否有 std:: 替代品?
在互联网上的一些搜索表明,您可以通过尝试以“输入”模式打开来手动添加存在检查:
std::fstream myfile("thefile.txt", std::ios::in);
if (myfile)
{
// error, file exists!
}
else
{
myfile.close();
myfile.open("thefile.txt", std::ios::out); // OK now
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)