C++ 如何使用 fstream fstream::app 标志附加到文件似乎不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4712192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:14:47  来源:igfitidea点击:

How to append to a file with fstream fstream::app flag seems not to work

c++fstream

提问by groovehunter

i simply want to write (append) to a logfile. I looked it up here:
http://www.cplusplus.com/reference/iostream/fstream/open/

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

so this is what i did

所以这就是我所做的

#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();

回答by AProgrammer

fstream::app|fstream::outinstead of fstream::app. appdoesn't make sense without specifying out(one could think it should have implied out, but it doesn't).

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