0

hello again,
so i want to make a programme
where the file saved is saved with different names at every loop..
like at first loop it will save as file1.txt, 2nd loop file2.txt, so on...
so this is my code...

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int x = 0;
    cout << "Hi" << endl;
    for (x == 0; x<10; x++){
        fstream file;
        file.open("trial", ios::out);
        file<<"save it";
        file.close();

    }
    cin.ignore();
    cout<<" DONE";
    return 0;
}

so this is what the code is, it will loop 10 times and it will save it as file1.txt, file2.txt, file3.txt......... file10.txt

please help, thanks

2
Contributors
1
Reply
14
Views
3 Years
Discussion Span
Last Post by deceptikon
0

You need to open the file with a new name each time:

for (int x = 0; x < 10; x++)
{
    stringstream filename;

    filename << "trial" << (x + 1) << ".txt";

    ofstream out(filename.str().c_str());

    out << "save it";
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.