Create numbered files

Hey There,

maybe it is pretty easy to do, but i cant make it for 2 days...
I want to create files "file1.dat", "file2.dat",... in dependency of integer counter N.

How do i create such strings and convert it into const char* for calling ofstream.open().

My testcode looks like:

1
2
3
4
5
6
7
int N=5; 
char s [10];
s=itoa(N,s,10);

const char* t="test"s".dat";

ofstream outfile(t);


but it obviously fails. I tried lot of other things, also using string library, but i dont get it ._.

Thanks for help!
1
2
3
4
5
int N=5; 

stringstream t;
t << "test" << N << ".dat";
ofstream outfile( t.str() );

recommandation: don't use capital letters for variables, they are usually used for consts/defines
this works for the string but the ofstream isnt working though.

Compiler (g++) sais: cannot convert basic_string to const char*
Last edited on
do this:

1
2
3
4
5
6
7
8
9
10
#include<string.h>
#include<stdlib.h>
void getname(int n,char* str)
{
	char temp[30];
	itoa(n,temp,10);
	strcpy(str,"file");
	strcat(str,temp);
	strcat(str,".dat");
}


pass the number and the string to store file name as parameters.
ok, i made it now with

1
2
3
4
5
6
7
int N=5; 

string temp;
stringstream t;
t << "test" << N << ".dat";
temp=t.str();
ofstream outfile( temp.data() );


thanks a lot
Topic archived. No new replies allowed.