Home > Software engineering >  why wont the loop repeat the file creation
why wont the loop repeat the file creation

Time:02-03

I'm new to c and wrote this code to generate 10 1MB files with incremented file names

#include <fstream>
#include <iostream>
#include <sstream>

using namespace std;

int main() {
    stringstream file_name_ss;

    std::ofstream file;
    for (int i = 0; i < 10;   i) {
        file_name_ss << "file_" << i << ".ini";
        string file_name = file_name_ss.str();
        int size = 1024 * 1024 * 1; //~1MB
        file.open( file_name, ios::app);
        for (int x = 0; x < size; x  ) {
            file << "a";
        }
    }
}

when I run it the program only one file (file_0.txt) is generated. is the second loop breaking/escaping the first?

I tried looking online and couldn't find anything that got past the first file

EDIT: I got the loops working(thank you Sam Varshavchik) by adding the line file.close(); to the inner loop. Here is my updated code:

#include <fstream>
#include <iostream>
#include <sstream>

using namespace std;

int main() {
    stringstream file_name_ss;

    std::ofstream file;
    for (int i = 0; i < 10;   i) {
        file_name_ss << "file_" << i << ".ini";
        string file_name = file_name_ss.str();
        int size = 1024 * 1024 * 1; //~1MB
        file.open( file_name, ios::app);
        for (int x = 0; x < size; x  ) {
            file << "a";
        file.close();
        }
    }
}

But I now have a new problem: now the file names look like this:

files

not a terrible error but still kind of annoying. I think it has something to do with this part:

file_name_ss << "file_" << i << ".ini";

CodePudding user response:

You have two problems, which are really the same problem but done twice: You only have a single string and file stream object, and those won't be "reset" when the loop iterates.

For example the single file object can only be opened once. Any attempt to open it again without closing in between will simply fail.

As for the string stream, each iteration will append to the file name you attempt to open. For example in the second iteration of the loop you will try to open the file file_0.inifile_1.ini. And so on.

The simple solution to both problems? Move the variable definitions inside the loop:

for (int i = 0; i < 10;   i) {
    ostringstream file_name_ss;
    file_name_ss << "file_" << i << ".ini";

    std::ofstream file(file_name_ss.str());

    // ... Rest of loop...
}

[Note that the code should really add some error checking.]

CodePudding user response:

stringstream file_name_ss;
std::ofstream file;
for (int i = 0; i < 10;   i) {
    file_name_ss << "file_" << i << ".ini";
    string file_name = file_name_ss.str();
    int size = 1; //~1MB
    file.open(file_name, ios::app);
    for (int x = 0; x < size; x  ) {
        file << "a";
    }
    file.close();//close file before open new one 
    file_name_ss.str("");//clear stream
}

CodePudding user response:

You need to close the file after you are done writing to it. Also, your code doesn't clear the previous file name so you just keep adding on to it. Add this to the end of your for loop.

file_name_ss.str("");
file.close();
  • Related