2011-01-05 10 views
0

파일을 동적으로 만들려고하는데 fstream을 사용할 방법이없는 것처럼 보입니다. 실제로 어떤 것이 있습니까?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(); 
} 
} 
+2

'(char) i + "sample_file.txt"'당신이 생각하는대로하지 않습니다. 이것은 Java/C#이 아닌 C++입니다. – heavyd

답변

5

런타임에 생성 된 이름을 의미하는 경우 올바른 이름으로 빌드해야합니다. 예 : (#include <sstream> 이후) :

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

outfile.open(fname.str().c_str());