title: Enumerating filenames in c++
body: <p>I want to generate  list of enumerated filenames</p>
  
  <pre><code>file1.dat
  file2.dat
  …
  </code></pre>
  
  <p>The following code</p>
  
  <pre><code>#include&lt;fstream&gt;
  for( int i=0; i&lt;5; i++) {
      std::ofstream fout( "file" + i + ".dat", std::ios );
      //do stuff
      fout.close();
  }
  </code></pre>
  
  <p>seems to be the natural implementation. Unfortunately, the integer <code>i</code> is incorrectly concatenated to the string; moreover, <code>ofstream</code> accepts a char* (not a string) as the file argument.
  The following</p>
  
  <pre><code>#include &lt;fstream&gt;
  #include &lt;sstream&gt;
  string toString(int&amp; i) {
      std::stringstream ss;
      ss &lt;&lt; i;
      return ss.str();
  }
  
  for( int i=0; i&lt;5; i++) {
      std::string fileName = "file" + toString(step) + ".dat";
      std::ofstream fout( (char*)fileName.c_str(), std::ios );
      //do stuff
      fout.close();
  }
  </code></pre>
  
  <p>works but is cumbersome. <code>(char*)fileName.c_str()</code> seems especially clumsy.
  Is there a better way to accomplish this task?</p>