在我的c++程序中,我使用sprintf和fstream将输出数据保存在一个文件中,如下所示
#include <iostream>
#include <fstream>
char outname[50];
int n = 100;
sprintf(outname, "output_%s_%d.dat", "file", n);
ofstream fout;
fout.open(outname);
如何使用std::sstring而不是sprintf获得文件名,并使用std::ofstream打开该文件?在上面的代码中,文件名是outname,它是用std::ofstream打开的。
发布于 2014-02-02 23:34:36
也许是这样的?
#include <sstream>
std::stringstream outname;
outname << "output_file_" << n << ".dat";
...
ofstream fout;
fout.open( outname.str().c_str() );
https://stackoverflow.com/questions/21521271
复制相似问题