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
How to append to a file with fstream fstream::app flag seems not to work
提问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::out
instead of fstream::app
. app
doesn'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
,但事实并非如此)。