title: C++ ofstream not creating and writing to file on subsequent runs of the program
body: <pre><code>#include &lt;iostream&gt;
  
  #define LOGFATAL(msg) log(0, msg)
  
  std::ofstream *logst = NULL;
  
  void log(int sev, char *msg)
  {   if (logst == NULL) {
          logst = new std::ofstream();
          logst-&gt;open("filea.txt", std::ios::out | std::ios::app);
          *logst &lt;&lt; "Logger started." &lt;&lt; std::endl;
      }
  
      std::ofstream &amp;_log = *logst;
      _log &lt;&lt; msg &lt;&lt; std::endl;
      _log.flush();
  }
  
  int main()
  {   LOGFATAL("Log msg1.");
      LOGFATAL("Log msg2.");
      LOGFATAL("Log msg3.");
  
      logst-&gt;close();
      delete logst;
  }
  </code></pre>
  
  <p>I am opening a file for logging the very first time I log and continue to keep it open until the end of program.</p>
  
  <p>Since I use the <code>flush()</code> operation after every log invocation, I expect to see my messages printed close to immediately. BUT THIS DID NOT HAPPEN. WHY?</p>
  
  <p>Currently, I kill my program using <kbd>Ctrl</kbd>+<kbd>C</kbd> before it finishes (Don't ask me why). On subsequent runs of the program I don't even see the log file getting created and even if it already exists I don't see any logs getting added. Since I don't let the <code>close()</code> execute, does the file descriptor get leaked and prevent future <code>open()</code> from new programs to fail? </p>
  
  <p>I am running it on a RHEL 7.2 and I assume most new OS's handle these days even if <code>close()</code> isn't called accidentally. Considering <kbd>Ctrl</kbd>+<kbd>C</kbd> is the only way to stop my program currently, what can I do to make my program to log correctly every time it is started?</p>
  
  <p>Is there a way from the system shell to check if there are any leaked file descriptors to my log file?</p>