title: c++ How can I read values from a .csv file and store them in a vector?
body: <p>I use Code::Blocks on Windows 7 to make little .exe from .cpp files, and I am a beginner (sorry!)</p>
  
  <p><strong>Here's today's problem:</strong> </p>
  
  <p>I have a .csv file containing long integers (from 0 to 2^16) separated by semicolons and listed as a series of horizontal lines.</p>
  
  <p>I will make a simple example here, but in reality the file can be up to 2Go big. </p>
  
  <p>Let's say my file  <code>wall.csv</code> appears like this in a text editor such as <code>Notepad++</code>:</p>
  
  <pre><code>350;3240;2292;33364;3206;266;290
  
  362;314;244;2726;24342;2362;310
  
  392;326;248;2546;2438;228;314
  
  378;334;274;2842;308;3232;356
  </code></pre>
  
  <p>Strangely enough, it appears like this in the windows <code>notepad</code></p>
  
  <pre><code>350;3240;2292;33364;3206;266;290
  362;314;244;2726;24342;2362;310
  392;326;248;2546;2438;228;314
  378;334;274;2842;308;3232;356
  </code></pre>
  
  <p>Anyway, </p>
  
  <p>let's say that I will know and will declare in 3 <code>float</code> variables the amount of columns, the amount of lines, and a value from the file.</p>
  
  <pre><code>int col = 7;   // amount of columns
  int lines = 4; // amount of lines
  float x = 0;     // a variable that will contain a value from the file  
  </code></pre>
  
  <p><strong>I want:</strong></p>
  
  <ol>
  <li>to create a <code>vector &lt;float&gt; myValues</code> </li>
  <li>do <code>myValues.push_back(x)</code> with each value from the <strong>1st line of the csv</strong></li>
  <li>do <code>myValues.push_back(x)</code> with each value from the <strong>2nd line of the csv</strong></li>
  <li>do <code>myValues.push_back(x)</code> with each value from the <strong>3rd line ...etc.</strong></li>
  </ol>
  
  <p><em>until the file has been entirely stored in the vector myValues.</em></p>
  
  <p><strong>My problem:</strong></p>
  
  <p>I don't know how to successively assign to the variable <code>x</code> the values present in the csv file.</p>
  
  <p>How should I do that?</p>
  
  <hr>
  
  <p>OK this code works (rather slowly but ok!):</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;vector&gt;
  using namespace std;
  
  int col = 1221;   // amount of columns
  int lines = 914; // amount of lines
  int x = 0;     // a variable that will contain a value from the file
  
  vector &lt;int&gt; myValues;
  
  int main() {
  
      ifstream ifs ("walls.tif.csv");
  
      char dummy;
      for (int i = 0; i &lt; lines; ++i){
          for (int i = 0; i &lt; col; ++i){
              ifs &gt;&gt; x;
              myValues.push_back(x);
              // So the dummy won't eat digits
              if (i &lt; (col - 1))
                  ifs &gt;&gt; dummy;
          }
      }
      float A = 0;
      float B = col*lines;
  
      for (size_t i = 0; i &lt; myValues.size(); ++i){
  
          float C = 100*(A/B);
          A++;
          // display progress and successive x values
          cout &lt;&lt; C &lt;&lt; "% accomplished, pix = " &lt;&lt; myValues[i] &lt;&lt;endl;
      }
  }
  </code></pre>