• Support PF! Buy your school textbooks, materials and every day products Here!

[C++] Dynamic File Naming

  • Comp Sci
  • Thread starter burritoloco
  • Start date
  • #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();
 

Answers and Replies

  • #2
Borek
Mentor
28,402
2,801
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.
 

Related Threads on [C++] Dynamic File Naming

Replies
6
Views
1K
Replies
0
Views
1K
Replies
2
Views
52
Replies
3
Views
581
Replies
1
Views
884
Replies
1
Views
720
  • Last Post
Replies
14
Views
941
  • Last Post
Replies
1
Views
309
  • Last Post
Replies
1
Views
2K
Replies
0
Views
1K
Top