C++写文件时覆盖与否的问题


ofstream out("/path/to/file");
out<<"write to file"< out.close();
使用上述方式,若文件不存在,自动创建新文件并写入“write to file”;若文件存在,会覆盖掉原文件的内容,写入“write to file”。

如果希望每次都写入文件末尾,而不覆盖原文件,可采用以下方式:

ofstream out(perf_count_path,ios::app);
out<<"write to file"< out.close();
使用ios::app实现追加写文件,不要忘记#include
 

你可能感兴趣的:(C++)