【发布时间】:2013-02-11 14:27:24
【问题描述】:
以下代码是在 Windows 7 x64 上使用 VC++ Nov 2012 CTP 编译的。
#include <fstream>
using namespace std;
int main()
{
ofstream fout("log.txt", ios::app|ios::trunc);
if (!fout)
{
cout << "An error occurred!" << endl; // Always go here! Why?
}
}
The cppreference.com website 并不是说ios::app 不能与ios::trunc 结合使用。
ios::app 和 ios::trunc 的确切语义是什么?
【问题讨论】:
-
文档没有说
ios::app不能和ios::trunc结合。 -
你想做什么? std::ios::trunc 基本上丢弃所有内容,而 std::ios::app 追加。
-
@CLearner,仅用于记录。删除旧内容并添加新文本。
-
@LightnessRacesinOrbit C++确实有一个官方文档:ISO 14882。当纯粹询问有关 C++ 的问题时,链接到其他网站是没有意义的。语言由标准定义。 (当然,许多问题涉及一个或多个编译器的细节,而不仅仅是语言。)