title: Can not successfully generate the std::set output to the file and console
body: <p>I am trying to read a list of integers from a file and store them to <code>std::set</code> container, after that want to output the integers to the console and then generate the output file. I have tried to do it but could not succeed. If I use <code>.erase()</code> in my program then I can see the output on the console and an empty file without text. If I do not use the <code>.erase()</code> then I have run time error of infinite loop. `</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;set&gt;
  #include &lt;fstream&gt;
  #include &lt;iterator&gt;
  using namespace std;
  
  int main()
  {
  
      set&lt;int&gt; myset;
  
      fstream textfile;
      textfile.open("input.txt");  // opening the file 
  
      // reading input from file 
      while (!textfile.eof())
      {
          int iTmp = 0;
          textfile &gt;&gt; iTmp;
          myset.insert(iTmp);
  
  
      }
  
      // output to the console
      set&lt;int&gt;::iterator iter = myset.begin();
      while (!myset.empty())
      {
          cout &lt;&lt; *myset.begin() &lt;&lt; " ";
          myset.erase(myset.begin());
      }
  
      // writting output to the file
  
      ofstream out_data("Ahmad.txt");
  
      while (!myset.empty())
      {
          out_data &lt;&lt; *myset.begin() &lt;&lt; " ";
      }
  
  
  
      system("pause");
  }`
  </code></pre>