0

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

3
Contributors
2
Replies
3
Views
11 Years
Discussion Span
Last Post by Micko
0

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.

0

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!

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.