0

ファイルを動的に作成しようとしていますが、fstream には方法がないようです。実際にはありますか?

#include <iostream>
#include <fstream>
using namespace std;

int main () {

for (int i = 0; i<2; ++i){
 std::ofstream outfile
 outfile.open ((char)i+"sample_file.txt"); // something like this. numbers appended to the filename
 outfile << i;
 outfile.close();
 }
}
4

1 に答える 1

5

実行時に作成された名前を意味する場合は、そうですが、有効な方法で名前を作成する必要があります。例 (後#include <sstream>):

std::ostringstream fname;
fname << "sample_file_" << i << ".txt";

outfile.open(fname.str().c_str());
于 2011-01-05T23:13:43.110 に答える