[C++] Dynamic File Naming

  • Comp Sci
  • Thread starter burritoloco
  • Start date
  • Tags
    Dynamic File
  • #1
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();
 
  • #2
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+.
 
  • #3
Thank you. Worked perfectly.
 

Suggested for: [C++] Dynamic File Naming

Replies
18
Views
689
Replies
1
Views
658
Replies
8
Views
886
Replies
2
Views
647
Replies
7
Views
697
Replies
2
Views
857
Replies
1
Views
628
Replies
14
Views
2K
Replies
2
Views
3K
Back
Top