C++ 不';fstream支持动态创建文件

C++ 不';fstream支持动态创建文件,c++,fstream,ofstream,C++,Fstream,Ofstream,我正在尝试动态创建文件,似乎无法使用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 app

我正在尝试动态创建文件,似乎无法使用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();
 }
}
#包括
#包括
使用名称空间std;
int main(){

对于(int i=0;i如果您的意思是使用在运行时创建的名称,那么是的,但您必须以有效的方式构建名称。例如,(在
之后#include
):

std::ostringstream fname;

fname如果您是指在运行时创建的名称,那么是的,但您必须以有效的方式构建名称。例如,(在
之后#include
):

std::ostringstream fname;
fNE<代码>(char)I+“SAMPLE文件.txt”< /C>不是按你的想法来做的。这是C++,而不是爪哇/C。<代码>(char)i+“SAMPLE文件.txt”< /C> >不是你想的那样。这是C++而不是爪哇/C。
std::ostringstream fname;
fname << "sample_file_" << i << ".txt";

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