我想在append
模式下打开文件,但如果文件已存在,请覆盖其内容。以追加模式打开文件,但截断文件
我已经试过这样的事情,但是这是不行的(我没有用太多的经验|和&运营商,因为你可以看到):
//does nothing
_fs.open(_path, std::ios_base::out | std::ios_base::app | std::ios_base::trunc);
然后,我决定开文件中out
模式,然后再次打开,但它append
模式之前关闭它,但当然,这似乎是一个不好的编程方式来做到这一点:
if(fexists(_path.c_str())){//Checks if the file exists
_fs.open(_path, std::ios::out);//Overrides the contents
_fs.close();
}
_fs.open(_path, std::ios_base::out | std::ios_base::app);
然后凭直觉我写了这样的事情:
_fs.open(_path, std::ios_base::out | std::ios_base::app & std::ios_base::out | std::ios_base::trunc);
现在它工作,为什么?
你为什么要附加一个文件,如果你只是打算无论如何要删除它,那就是截断点模式。另外,你不能在'std :: ios :: app'和'std :: ios :: trunc'模式下打开一个文件 – Kevin 2014-09-04 16:28:02