gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:52:59 25 4
gpt4 key购买 nike

我有这样一个功能:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com