title: C++ efficient way to write to file multiple times
body: <p>I am dealing with a callback function where based on the data in the callback, I want to write to different files.</p>
  
  <p>For example, in one call, I might want to write to january.csv while in another call with different data, it might be july.csv instead. There is no pre-determined sequence, it could be any month in each callback and I have no way of knowing in advance. january.csv (all the months actually) will get written to multiple times.</p>
  
  <p>These callbacks are happening extremely rapidly so I need this code to be as efficient as possible.</p>
  
  <p>The naive approach I would take would be to use the following code each time:</p>
  
  <pre><code>ofstream fout;
  fout.open(month_string);
  fout&lt;&lt;data_string&lt;&lt;endl;
  fout.close();
  </code></pre>
  
  <p>The problem is that this doesn't seem very efficient since I am continuously opening/closing the month.csv file. Is there a faster way where I can say keep january.csv, february.csv, etc open all the time to make this faster?</p>
  
  <p>EDIT: I am writing to /dev/shm on linux so I/O delays are not really a problem.</p>