Reputation:
I want to create thousands of file with txt extension in c++. Is it possible? I can not imagine how can i do that. If I write some code right here so you can understand what I want.
int main ()
{
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
I want to generate a bulk of txt of files with this method. In addition I want to run these codes inside a for structure. At the end of the operation I must have 10,000 file and these file's names are like example1.txt , example2.txt , example3.txt ... You do not have to write code I just want to know how to do it. You can just share link to some tutorial.
Upvotes: 0
Views: 118
Reputation: 13097
#include <fstream>
#include <iostream>
int main () {
std::ofstream myfile;
for(int i=1;i<=1000;i++){
myfile.open("example" + std::to_string(i) + ".txt");
myfile << "Writing this to a file.\n";
myfile.close();
}
return 0;
}
Upvotes: 1
Reputation: 206717
Yes, you can do that. Here are the steps I suggest:
In each iteration of the loop construct the name of a unique file by using the loop counter. You can use std::ostringstream
or sprintf
for that.
Use the code you already have to create a file in each iteration of the loop.
Upvotes: 2