【问题标题】:How to append to a file with fstream fstream::app flag seems not to work如何使用 fstream fstream::app 标志附加到文件似乎不起作用
【发布时间】:2011-06-10 09:03:07
【问题描述】:

我只是想写(追加)到一个日志文件。我在这里查到了:
http://www.cplusplus.com/reference/iostream/fstream/open/

这就是我所做的

#include <fstream>

fstream outfile;

//outfile.open("/tmp/debug.txt" );  // works, simply for writing
outfile.open("/tmp/debug.txt", fstream::app );  // does nothing

outfile << "START" << endl;

outfile.close();

【问题讨论】:

  • 您也可以使用 ofstream,您的代码可以使用它。

标签: c++ fstream


【解决方案1】:

fstream::app|fstream::out 而不是fstream::app。如果不指定 outapp 就没有意义(有人可能认为它应该隐含 out,但事实并非如此)。

【讨论】:

  • 如果您不提供参数,fstream 将提供默认值,但如果您提供了一个参数(如应用程序),它不会自动或您的默认值。
  • fstream::app 不一定表示输出追加,但ofstream::app 表示输出追加(因为ofstream)。也就是说,是流类型而不是开启方式。
猜你喜欢
  • 1970-01-01
  • 2013-02-09
  • 2020-12-08
  • 1970-01-01
  • 2012-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多