c++ - 创建用于在函数内写入的文件的最简单方法?

标签 c++ io

我有这样一个功能:

void my_func(unordered_map<std::string, std::string> arg){

    //Create/open file object on first call and append to file on every call

    //Stuff
}

在这个函数中我想写入一个文件。如何在不必在调用方中创建文件对象并将其作为参数传递的情况下实现此目的?每次调用该函数时,我都想将最新的写入附加到文件末尾。

最佳答案

void my_func(unordered_map<std::string, std::string> arg){

    static std::ofstream out("output.txt");
    // out is opened for writing the first time.
    // it is available for use the next time the function gets called.
    // It gets closed when the program exits.

}

关于c++ - 创建用于在函数内写入的文件的最简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23686069/

相关文章:

c - SIGIO从不开火

haskell - 将 Kleisli 箭头提升到 IO?

c++ - 来自文件 C++ BorlandC 3.1 的字符串

c++ - 如何在 Qt 中以图形方式绘制回旋曲线?

c++ - C++中如何解析 `auto a(b);`?

c++ - 如何使用 Boost 库序列化结构中的 union ?

java - ByteArrayInputStream 到 ObjectInputStream 消失了

c++ - 访问从模板类派生的类中的基本成员函数

linux - Linux 上 POSIX AIO 和 libaio 的区别?

c++ - 检测文件 C++ 的最后一行