# [C++] Dynamic File Naming

• Comp Sci
burritoloco
Hello,

I'm wondering how to name/open a file whose name is given by a string with an integer appended at the end, i.e., something in the style of the following. Thank you!

int i = 5;

ofstream fout;

string s = "Hello " + i;

fout.open(s);

// write something to the file

fout.close();

Mentor
I would take

Code:
char filename[32];

sprintf(filename,"name%i",i);

fout.open(filename);

route, but I am not convinced it is how it should be done in C++. I started with C and have tendency to code in C+.

burritoloco
Thank you. Worked perfectly.