title: The file was set up right but eof() and peek() return wrong value from new stream unexpectedly
body: <p>I want to generate a random array.</p>
  
  <pre><code>void Random_nums(int n, string fileName) {
  
      ofstream fsave(fileName);
      int ranArray[n];
  
      srand( (unsigned int)time(NULL) );
      for(int i=0; i&lt;n; i++)
      {
          ranArray[i]=rand()%2001 - 1000;
          fsave &lt;&lt; ranArray[i] &lt;&lt; '\n'; //save them into ranArray
      }
  
      fsave.close();
  }
  </code></pre>
  
  <p>And I want to save them into a separate file. </p>
  
  <p>After that I want to read the data I just saved and implement them in a new function.</p>
  
  <pre><code>void FunctionWantToUse(string inputFile, string outputFile) {
  
      //reading data
      ifstream fin(inputFile);
      cout &lt;&lt; fin.is_open();//which is 1
  
      //how many numbers are there in this file
      int n = 0;
      n = distance(istream_iterator&lt;int&gt;(fin), istream_iterator&lt;int&gt;());
  
      //restore the array
      int array[n];
  
      //test
      cout &lt;&lt; fin.peek() &lt;&lt; ' ' &lt;&lt; EOF;//they are both -1!
  
      //another test
      cout &lt;&lt; fin.peek() &lt;&lt; '\n'; //which return -1
      cout &lt;&lt; fin.eof() &lt;&lt; '\n'; //which return 1
  
      //since peek() and EOF both return -1, I can't get into this loop. 
      while (fin.peek() != EOF) {
          if ((fin &gt;&gt; array[i]).good()) {
              cout &lt;&lt; array[i];
          }
      }
  </code></pre>
  
  <p>Completely have no idea why the value of fin.peek() and fin.eof() change to -1 and 1 respectively.</p>
  
  <p>And the console told me this as I run these two functions.</p>
  
  <pre><code>-1 -1-1
  1
  
  Process finished with exit code 0
  </code></pre>
  
  <p>I also give some other tests, such as, place fsave.eof() in the loop when I generate random number, and it prints out all 0s.</p>
  
  <p>Could someone please tell me what's going on here? I completely have no clue right now.</p>