简体   繁体   中英

Is it possible to create bulk of txt files in c++?

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.

Yes, you can do that. Here are the steps I suggest:

  1. Use a for or a while loop.

  2. 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.

  3. Use the code you already have to create a file in each iteration of the loop.

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM