ofstream out("/path/to/file");
out<<"write to file"<<endl;
out.close();
使用上述方式,若文件不存在,自动创建新文件并写入“write to file”;若文件存在,会覆盖掉原文件的内容,写入“write to file”。
如果希望每次都写入文件末尾,而不覆盖原文件,可采用以下方式:
ofstream out(perf_count_path,ios::app);
out<<"write to file"<<endl;
out.close();使用ios::app实现追加写文件,不要忘记#include <iostream.h>
本文详细介绍了如何使用C++中的ofstream类进行文件读写操作。包括在文件不存在时自动创建新文件、覆盖已有文件内容,以及如何实现追加写文件的方法。同时提醒读者在追加写文件前需要包含<iostream.h>头文件。
7175

被折叠的 条评论
为什么被折叠?



