title: How to save .txt file in C++?
body: <p>I have created a code writing stuff in a .txt file and read from it. But if I close the program and start to write again, it deletes the old text and overwrites it with the new one.</p>
  
  <p>Is there a way to not overwrite existed data?</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  void check() {
  
      string text;
      ifstream file;
      file.open("test.txt");
      getline(file, text);
  
      if (text == "") {
          cout &lt;&lt; "There's no data in file" &lt;&lt; endl;
      } else {
          cout &lt;&lt; "File contains:" &lt;&lt; endl;
          cout &lt;&lt; text &lt;&lt; endl;
      }
  }
  
  int main() {
  
      check();
  
      string text;
      ofstream file;
      file.open("test.txt");
      cout &lt;&lt; "Type some text" &lt;&lt; endl;
      getline(cin, text);
      file &lt;&lt; text;
  
      return 0;
  }
  </code></pre>