我之前问过 there was a combination of openmode to avoid modifications of an existing file .现在我想知道相反的情况是否可能:
- 如果文件已经存在,截断它 (
std::ios::trunc
) - 如果文件不存在,什么都不做
是否有 std::ios::openmode
可以用 std::ofstream
来实现?
最佳答案
据我所知,没有这样的开放模式(它看起来不像是一个非常常见的用例)。我猜你可以先检查文件是否存在,只有存在,才用 trunc
打开模式打开它。
关于c++ - std::ios::openmode 的组合在文件存在时截断但阻止创建新文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16624960/