Hello
Im trying to segment a file. In doing so I need to open/createnew smaller files.

The code I have

[ initial.open("c:/test.jpg", ios::in|ios::binary); // input
output.open("c:/copy.jpg", ios::out|ios::binary); // output]

only creates one file output. I would like to have a for loop which creates n file eg text1, text 2, text3 ... textn but i have no idea.

CAn somebody help

you already said you need a loop, so I suppose you know how to do that. Inside the loop you need to format the filename based on the loop counter.

char filename[255];

// assum loop counter is variable i

sprintf(filename,"file%d.jpg",i);
utput.open(filename, ios:out|ios::binary); // output]

There are a couple of other ways too, but I think the above is about as simple and easy as it gets.

How about something like this:

#include <iostream>
#include <sstream> 
#include <string>
#include <fstream>
using namespace std;
 
int main ()
{
    
    int i;
    string str = "test", str2;
    ofstream files_array[10];
    for (i = 0; i < 10; i++)
    {
        stringstream sstr;
        sstr << str << i;
        files_array[i].open(sstr.str().c_str());
    }
    
    return 0;
}

I hope it helps!

And Ancient Dragon's approach is much simpler but here's another, more complicated, way!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.