title: Why file.write() modifying the content of file in Python
body: <p>Can anyone explain why the content of the file is being changed while writing?</p>
  <p>And how to fix this</p>
  <pre><code>c='''#include &lt;iostream&gt;
  
  int 
  main ()
  {
    std::cout &lt;&lt; &quot;Hello, world!\n&quot;; &lt;== this line is getting changed in output
    return 0;
  }
  '''
  with open('x.cpp','w+') as f:
      f.write(c)
  </code></pre>
  <p>The output of the code</p>
  <pre><code>#include &lt;iostream&gt;
  
  int 
  main ()
  {
    std::cout &lt;&lt; &quot;Hello, world!
  &quot;; &lt;= modified while doing write operation
    return 0;
  }
  </code></pre>
  <p>I am using Python 3.8</p>
  <p>What I am doing wrong and how can I fix this?</p>
  <p>Thank you</p>