title: Can I cast an ostream as an ofstream?
body: <p>I have this code</p>
  
  <pre><code>ostream &amp; operator&lt;&lt;(ostream &amp; out, call_class &amp; Org)
  {
      for (int i = 0; i&lt;Org.count; i++)
      {
          out &lt;&lt; Org.call_DB[i].firstname &lt;&lt; "  " &lt;&lt; Org.call_DB[i].lastname
              &lt;&lt; "  " &lt;&lt; Org.call_DB[i].relays &lt;&lt; "  " &lt;&lt; Org.call_DB[i].cell_number
              &lt;&lt; "  " &lt;&lt; Org.call_DB[i].call_length &lt;&lt; endl;
      }
  
      //Put code to OPEN and CLOSE an ofstream and print to the file "stats7_output.txt".
  
  
      return out;  //must have this statement
  }
  </code></pre>
  
  <p>As it says i need to use an <code>ofstream</code> to print the contents of <code>out</code> to a file, can I just cast <code>out</code> as an <code>ofstream</code> and use that?</p>
  
  <p>For example, would this work?</p>
  
  <pre><code>new_stream = (ofstream) out;
  </code></pre>
  
  <p>This is the code in the main function that calls this function</p>
  
  <pre><code>cout &lt;&lt; MyClass &lt;&lt; endl;
  </code></pre>
  
  <p>MyClass is of type call_class
  Assume that I cannot just change the parameter <code>out</code> to be of type <code>ofstream</code></p>
  
  <p>EDIT: Thank you for your help everybody. I know this was a difficult question to answer due to lack of clear instruction. My professor basically gave us two different versions of the same question and it was unclear what he was asking. I just wrote the data to a file outside of the function, hopefully that will be good enough.</p>