title: How to read a file into vector in C++?
body: <p>I need to read from a <code>.data</code> or <code>.txt</code> file containing a new <code>float</code> number on each line into a vector. </p>
  
  <p>I have searched far and wide and applied numerous different methods but every time I get the same result, of a <code>Main.size()</code> of <code>0</code> and an error saying <code>"Vector Subscript out of Range"</code>, so evidently the vector is just not reading anything into the file.</p>
  
  <p>Note: the file is both in the folder and also included in the VS project.</p>
  
  <p>Anyway, here's my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  int main() {
  
      vector&lt;double&gt; Main;
      int count;
      string lineData;
      double tmp;
  
      ifstream myfile ("test.data", ios::in);
  
      double number;  
  
      myfile &gt;&gt; count;
      for(int i = 0; i &lt; count; i++) {
          myfile &gt;&gt; tmp;
          Main.push_back(tmp);
          cout &lt;&lt; count;
      }
  
      cout &lt;&lt; "Numbers:\n";
      cout &lt;&lt; Main.size();
      for (int i=0; i=((Main.size())-1); i++) {
          cout &lt;&lt; Main[i] &lt;&lt; '\n';
      }
  
      cin.get(); 
      return 0;
  }
  </code></pre>
  
  <p>The result I get is always simply:</p>
  
  <pre><code>Numbers:
  0
  </code></pre>