title: Can you safely close a file that was never opened?
body: <p>If I have a <a href="http://en.cppreference.com/w/cpp/io/basic_fstream"><code>std::ofstream</code></a> that may or may not have been opened, is it safe to try to <code>close</code> regardless? In otherwords does <code>close()</code> do anything nasty (throw exception, etc) if <code>!is_open()</code>. For example</p>
  
  <pre><code>std::ofstream out;
  if (some_condition)
  {
      out.open(path, std::ios::out);
  }
  </code></pre>
  
  <p>After I'm done with the file, can I just say</p>
  
  <pre><code>out.close();
  </code></pre>
  
  <p>Or should I first check</p>
  
  <pre><code>if (out.is_open())
      out.close();
  </code></pre>
  
  <p>The only description of <a href="http://en.cppreference.com/w/cpp/io/basic_fstream/close"><code>std::basic_fstream::close</code></a> on cppreference is  </p>
  
  <blockquote>
    <p>Closes the associated file.<br>
    Effectively calls <code>rdbuf()-&gt;close()</code>. If an error occurs during operation, <code>setstate(failbit)</code> is called.</p>
  </blockquote>