title: How to read and write in file with `fstream` simultaneously in c++?
body: <p>I have the following code</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;fstream&gt;
  #include&lt;string&gt;
  
  using namespace std;
  
  int main(void) {
      fstream ofile;
      ofile.open("test.txt", ios::in | ios::out | ios::app);
      for(string line; getline(ofile, line) ; ) {
          cout &lt;&lt; line &lt;&lt; endl;
      }
      ofile &lt;&lt; "stackexchnange" &lt;&lt; endl;
      ofile.close();
      return 0;
  }
  </code></pre>
  
  <p><code>test.txt</code> contains</p>
  
  <pre><code>hello world!
  stackoverflow
  </code></pre>
  
  <p>Above code outputs</p>
  
  <pre><code>hello world!
  stackoverflow
  </code></pre>
  
  <p>And after running the code <code>stackexchange</code> is not appending at the end of <code>test.txt</code>. How to read and then write in file?</p>