title: C++ - ofstream doesn't output to file until I close the program
body: <p>I have the following code:</p>
  
  <pre><code>   ofstream mOutFile.open(logPath, ios_base::app);
     string lBuilder;
  
     lBuilder.append("========================================================\n");
     lBuilder.append("Date: ");
     lBuilder.append(asctime(timeinfo));
     lBuilder.append("\n");
     lBuilder.append("Log Message:\n");
     lBuilder.append(toLog);
     lBuilder.append("\n");
     lBuilder.append("========================================================\n\n");
  
     int lSize = lBuilder.size();
     char* lBuffer = new char[lSize];
     int index = 0;
     for each (char c in lBuilder)
        lBuffer[index++] = c;
  
     mOutFile.write(lBuffer, lSize);
     mOutFile.flush();
  </code></pre>
  
  <p>Unfortunately, until I close the app (I assume that closing the ofstream would work as well) the output does not get written to the text file. I know I could probably close and reopen the stream and everything will "just work" but that seems like a silly and incorrect solution. What am I doing wrong here?</p>
  
  <p>I have also tried the following variations based on other questions I have found here, but these solutions did not work:</p>
  
  <pre><code>mOutputFile &lt;&lt; flush;
  mOutputFile &lt;&lt; endl;
  </code></pre>
  
  <p>Thanks in advance for any assistance on this.</p>
  
  <p><em>edit</em> Everything in this code is working visual c++, it builds and works fine except the file is not written to until the stream is closed, even if I force a flush. Also, I switched from using the &lt;&lt; operator to the char * and .write () to see if anything behaved differently.</p>