我想创建名称由 int 变量 + ".txt"
组成的文件。我已经试过了:
std::string filename = i + ".txt";
sprintf
不是最好的方法 - 至少在我看来是这样,因为它需要一个带有分配内存的 C
风格的字符串
sprintf( tmp, "%d.txt", i);
std::string filename = tmp;
也许 itoa
是最好的选择?
最佳答案
你可以使用字符串流
#include <sstream>
...
std::stringstream ss;
ss << i << ".txt";
std::string filename = ss.str();
关于c++ - 以文件名作为变量打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9364094/