title: C++ Write sorted contents of vector<string> to file
body: <p>This currently reads a .txt file and sorts the contents. I'm trying to get it to write those sorted contents of the vector to a file. Currently it only writes one line, how can I can get it to put all lines in the new file? Thank you so much. -Kaiya</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  #include &lt;cmath&gt;
  #include &lt;fstream&gt;
  using namespace std;
  inline void keep_window_open() {char ch; cin&gt;&gt;ch;}
  
  int main()
  {
      string line;
      ifstream myfile("weblog.txt");
      vector&lt;string&gt; fileLines;
  
      //stack overflow example
      if (!myfile) //test the file
      {
          cout &lt;&lt; "Unable to open the file" &lt;&lt; endl;
          return 0;
      }
  
      while (getline(myfile, line))
      {
          fileLines.push_back(line);
          //cout &lt;&lt; line &lt;&lt; '\n';
      }
  
      sort(fileLines.begin(), fileLines.end()); //sorting string vector
  
      for (string &amp;s : fileLines)
      {
          cout &lt;&lt; s &lt;&lt; " ";
          ofstream newfile ("newfile.txt");
          newfile &lt;&lt; s &lt;&lt; " ";
      };
  
      return 0;
  }
  </code></pre>