title: Reading data from .txt file and saving into multiple vectors using c++
body: <p>I have a data.txt file that contains data like this:</p>
  
  <pre><code>1.0 0.5 1.5 0.01761
  2.0 1.5 2.5 0.01977
  3.0 2.5 3.5 0.02185
  </code></pre>
  
  <p>I am trying to read the data.txt file and storing the numbers in the individual columns into different vectors. For example, the 1.0, 2.0 and 3.0 of the left-most column will go into a <code>vector&lt;double&gt; w</code>, 0.5, 1.5 and 2.5 in the second column will go into <code>vector&lt;double&gt; x</code>. The remaining 2 columns will go into <code>vector&lt;double&gt; y</code> and <code>vector&lt;double&gt; z</code> respectively.</p>
  
  <p>I have read other replies to similar questions such as <a href="https://stackoverflow.com/questions/13686604/reading-data-from-a-txt-file-and-saving-to-a-vector">this</a> and <a href="https://stackoverflow.com/questions/7151922/reading-a-txt-text-file-in-c-containing-float-separated-by-space">this</a>. However, they do not address the issue of storing data from different columns into different vectors.</p>
  
  <p>The following is my current attempt:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;vector&gt;
  
  using namespace::std;
  
  int main()
  {
      FILE *f_read;
      double my_variable = 0;
      vector&lt;double&gt; w, x, y, z;
      f_read= fopen("data.txt", "r");
  
      for(int i = 0; i &lt;= 3; ++i)
      {
          fscanf(f_read, "%.lf", &amp;my_variable);
          w[i] = my_variable;
          fscanf(f_read, "%.lf", &amp;my_variable);
          x[i] = my_variable;
          fscanf(f_read, "%.lf", &amp;my_variable);
          y[i] = my_variable;
          fscanf(f_read, "%.lf", &amp;my_variable);
          z[i] = my_variable;
      }
  
      //the following loop is printed to verify the vectors are correctly filled
      for(int j = 0; j &lt;=3; ++j)
      {
          cout &lt;&lt; "The " &lt;&lt; j &lt;&lt; "th value in w is " &lt;&lt; w[j] &lt;&lt; endl;
          cout &lt;&lt; "The " &lt;&lt; j &lt;&lt; "th value in x is " &lt;&lt; x[j] &lt;&lt; endl; 
          cout &lt;&lt; "The " &lt;&lt; j &lt;&lt; "th value in y is " &lt;&lt; y[j] &lt;&lt; endl;
          cout &lt;&lt; "The " &lt;&lt; j &lt;&lt; "th value in z is " &lt;&lt; z[j] &lt;&lt; endl;
      }
  return 0;
  }
  </code></pre>
  
  <p>However, the last "for" loop was never printed and the program returned -1073741819. My guess is something is wrong with how I fill the vectors in the first loop, but I am unable to see where my error is.</p>
  
  <p>I will appreciate any help. Thank you!</p>