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();
}
}
I'm trying to create files dynamically and it seems like there is no way with fstream. Is there actually any?
#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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的意思是在运行时创建的名称,那么是的,但您必须以有效的方式构建您的名称。例如(在
#include
之后):If you mean with a name created at run time, then yes, but you have to build your name in a valid way. E.g. (after
#include <sstream>
):