1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[C++] Dynamic File Naming

  1. Sep 20, 2012 #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. jcsd
  3. Sep 20, 2012 #2

    Borek

    User Avatar

    Staff: Mentor

    I would take

    Code (Text):
    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+.
     
  4. Sep 20, 2012 #3
    Thank you. Worked perfectly.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [C++] Dynamic File Naming
  1. Dynamics ? (Replies: 2)

  2. Fluid dynamics (Replies: 2)

Loading...