title: How to write and read a file with `fstream` simultaneously in c++?
body: <p>I'm trying to write some text to a file and then read it using only 1 fstream object.</p>
  
  <p>My question is very similar to <a href="https://stackoverflow.com/q/32435991/9157799">this question</a> except for the order of the read/write. He is trying to read first and then write, while I'm trying to write first and then read. His code was able to read but did not write, while my code is able to write but not read.</p>
  
  <p>I've tried <a href="https://stackoverflow.com/a/32437476/9157799">the solution</a> from his question but it only works for read-write not write-read.</p>
  
  <p>Here is my code:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  int main()
  {
      fstream fileObj("file.txt", ios::out|ios::in|ios::app);
  
      // write
      fileObj &lt;&lt; "some text" &lt;&lt; endl;
  
      // read
      string line;
      while (getline(fileObj, line))
          cout &lt;&lt; line &lt;&lt; endl;
  }
  </code></pre>
  
  <p>The code writes <code>some text</code> to <code>file.txt</code> successfully but it doesn't output any text from the file. However, if I don't write text to the file (remove <code>fileObj &lt;&lt; "some text" &lt;&lt; endl;</code>), the code will output all text of the file. How to write first and then read the file?</p>