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