Hi everyone.
Im new in C++, actually im doing my first project, and i have a few questions, if you guys could help me it would be awesome. :)

In a method i have to create a .txt file, It have a parameter indicating the number of the file. But the parameter is an int variable and i need to change it to char* and then concatenate it to more char* variables.

void Labyrinth::createFile(int numberFile){
    char* numFile = (char*)numberFile;  
    char* namePt1 = "Labyrinth";
    char* namePt2 = ".txt";
    const char* finalName = HERE I NEED TO CONCATENATE namePt1+numFile+namePt2

    ofstream fileLabyrinth;
    fileLabyrinth.open(finalName);
}

I know the file.open() needs a const char* var.
So, I dont know how to change the int var to char*, and then concatenating it to two or more char*.

Thanks.

Recommended Answers

All 2 Replies

There are several ways to solve that problem. IMHO this is the simplest.
sprintf() is your friend here

char filename[255]; // final file name
 char* namePt1 = "Labyrinth";
int num = 5;
sprintf(filename,"%s%d.txt", namePt1,num);

ofstream fileLabyrinth(filename);

Alternate method is pure c++

string filename;
string namePt1 = "Labyrinth";
int num = 5;
filename = namePt1;
stringstream str;
str << num;
filename += str.str();
filename += ".txt";

ofstream fileLabyrinth(filename.c_str());
commented: He help me very well! :) +0

Yes! Now it works
Thanks man! =)

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.