title: C++ How do i add a string to an existing text file without overwriting it?
body: <p>I have coded a programm which can load one text file, can decide how long each word is and can write txt files based on the length of the words. But when i run the programm, the new text files are always filled with just one word(each new word with an already existing text file for his length just overrides the text file)</p>
  
  <p>The start text file looks like this():</p>
  
  <p><a href="http://i.stack.imgur.com/WBaRf.png" rel="nofollow">http://i.stack.imgur.com/WBaRf.png</a></p>
  
  <p>My new created text files(named after their length for example: 7.txt) after i runned the programm:</p>
  
  <p><a href="http://i.stack.imgur.com/6QKgE.png" rel="nofollow">http://i.stack.imgur.com/6QKgE.png</a></p>
  
  <p>My code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  
  using namespace std;
  
  int main(int argc, char *argv[])
  {
      char     filename[128];
      ifstream file;
      char     line[100];
  
  cout &lt;&lt; "Input filename: " &lt;&lt; flush;
  cin.getline(filename, 127);
  
  file.open(filename, ios::in);
  
  if (file.good())
  {
      file.seekg(0L, ios::beg);
      int number = 0;
      while (!file.eof())
      {
          file.getline(line, 100);
          stringstream stream;
          stream &lt;&lt; line;
          number++;
          cout &lt;&lt;"Number: "&lt;&lt; number &lt;&lt; " length: " &lt;&lt; stream.str().length() &lt;&lt; " " &lt;&lt; line &lt;&lt; endl;
          std::stringstream sstm;
          int laenge = stream.str().length();
          string txt = ".txt";
          sstm &lt;&lt; laenge &lt;&lt; txt;
          string result = sstm.str();
          std::ofstream outFile(result);
          outFile &lt;&lt; line &lt;&lt; endl;
          outFile.close();
      }
  }
  else
  {
      cout &lt;&lt; "File not found" &lt;&lt; endl;
  }
  
  while (true)
  {
  };
  
  return 0;
  </code></pre>
  
  <p>}</p>
  
  <p>My goal is that i have sorted the whole words into the file their files, the only problem is that they overwrite themself... How can i get rid off that?</p>