通过boost追加到现有文件

Appending to existing file via boost

本文关键字:文件 boost 追加 通过      更新时间:2023-10-16

我需要将许多日志文件聚合到一个日志中。

我曾尝试使用boost::filesystem::copy_file来完成此操作,但它不支持追加。

有什么想法吗?(我更喜欢通过boost库来做这件事)

Tnx

这个简单的任务不需要Boost-标准的iostream就能完成任务:

#include <fstream>
//...
using std::ifstream;
using std::ofstream;
ifstream input1("input1.log"), input2("file2.log");
// append to an existing file
ofstream output("output.log", ofstream::out | ofstream::app);
output << input1.rdbuf() << input2.rdbuf();
//...

(但请注意,上述方法可能具有次优性能;请查看此答案以了解如何提高性能。)