我有这样一个功能:
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/