title: How to write to middle of a file in C++?
body: <p>I think this should be quite simple, but my googling didn't help so far... I need to write to an existing file in C++, but not necessarily at the end of the file.</p>
  
  <p>I know that when I just want to append text to my file, I can pass the flag <code>ios:app</code> when calling <code>open</code> on my stream object. However, this only let's me write to the very end of the file, but not into its middle.</p>
  
  <p>I made a short program to illustrate the issue:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  int main () {
  
    string path = "../test.csv";
  
    fstream file;
    file.open(path); // ios::in and ios::out by default
  
    const int rows = 100;
    for (int i = 0; i &lt; rows; i++) {
      file &lt;&lt; i &lt;&lt; "\n";
    }  
  
    string line;
    while (getline(file, line)) {
      cout &lt;&lt; "line: " &lt;&lt; line &lt;&lt; endl; // here I would like to append more text to certain rows
    }
  
  
    file.close();
  
  }
  </code></pre>