且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我怎么可以追加到一个文本文件?

更新时间:2023-12-03 08:28:04

打开文件时包括 O_APPEND 标记。请参阅 引用页_open()

Include the O_APPEND flag when opening the file. See the reference page for _open().

由于这是C ++考虑使用 的ofstream 代替。这是类型安全的,除去具有指定的参数的长度被写入到文件的要求:

As this is C++ consider using an ofstream instead. These are type-safe and remove the requirement of having to specify the length of the arguments being written to the file:

std::ofstream out(full_path, std::ios_base::app);
if (out.is_open())
{
    out << "----Session----\n\n"
        << "Date/Time: " << datetime << "\n\n"
        << "Text: " << text << "\n\n\n\n";
}