title: behavior of C++ ofstream in the event of program crash
body: <p>I am trying to debug a run time error that is causing my program to freeze up in the middle of execution. I have created a logging system that writes a simple log text file during the course of execution. </p>
  
  <p>I write information to the log file simply using an ofstream object's stream insertion (&lt;&lt;) operator with messages of std::string type. </p>
  
  <pre><code>void foo(){
  // ... Code block 1 ...
  // myLogger.Write (timestamp, "Code block1 successfully executed");
  // ... Code block 2 ... PROGRAM FREEZE AND CRASH!
  }
  </code></pre>
  
  <p>My question is this : Suppose my program freezes up, and I need to kill it's process from the Task Manager, can I trust my logging system to have reliably written all messages prior to the point of crash?</p>
  
  <p>Or, in reference to the above mentioned code, if Code block2 causes my program to freeze up, can I trust that the "Code block1 successfully executed" message will in every case indeed show up in my log file?</p>
  
  <p>P.S. I am on Windows, and the reason I feel concerned is I don't fully trust the OS behavior around file permissions in odd cases. My program does have read/write permission to the file, but I don't know what odd things can happen during crash. Maybe I am just paranoid, please tell me if so.</p>
  
  <p>Is there a better way to detect what code block the crash happens in?</p>