title: c++ program for reading an unknown size csv file (filled only with floats) with constant (but unknown) number of columns into an array
body: <p>was wondering if someone could give me a hand im trying to build a program that reads in a big data block of floats with unknown size from a csv file. I already wrote this in MATLAB but want to compile and distribute this so moving to c++.</p>
  
  <p>Im just learning and trying to read in this to start</p>
  
  <pre><code>7,5,1989
  2,4,2312
  </code></pre>
  
  <p>from a text file.</p>
  
  <p>code so far.</p>
  
  <pre><code>// Read in CSV
  //
  // Alex Byasse
  
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  #include &lt;stdlib.h&gt;
  
  int main() {
  
      unsigned int number_of_lines = 0;
      FILE *infile = fopen("textread.csv", "r");
      int ch;
      int c = 0;
      bool tmp = true;
      while (EOF != (ch=getc(infile))){
        if(',' == ch){
      ++c;
        }
        if ('\n' == ch){
      if (tmp){
        int X = c;
        tmp = false;
      }
              ++number_of_lines;
      }
      }
      fclose(infile);
  
    std::ifstream file( "textread.csv" );
  
    if(!file){
      std:cerr &lt;&lt; "Failed to open File\n";
      return 1;
    }
  
    const int ROWS = X;
    const int COLS = number_of_lines;
    const int BUFFSIZE = 100;
    int array[ROWS][COLS];
    char buff[BUFFSIZE];
    std::string line; 
    int col = 0;
    int row = 0;
    while( std::getline( file, line ) )
    {
      std::istringstream iss( line );
      std::string result;
      while( std::getline( iss, result, ',' ) )
        {
          array[row][col] = atoi( result.c_str() );
          std::cout &lt;&lt; result &lt;&lt; std::endl;
          std::cout &lt;&lt; "column " &lt;&lt; col &lt;&lt; std::endl;
          std::cout &lt;&lt; "row " &lt;&lt; row &lt;&lt; std::endl;
          col = col+1;
      if (col == COLS){
      std:cerr &lt;&lt; "Went over number of columns " &lt;&lt; COLS;
      }
        }
      row = row+1;
      if (row == ROWS){
        std::cerr &lt;&lt; "Went over length of ROWS " &lt;&lt; ROWS;
      }
      col = 0;
    }
    return 0;
  }
  </code></pre>
  
  <p>My matlab code i use is >></p>
  
  <pre><code>fid = fopen(twoDM,'r');
  
  s = textscan(fid,'%s','Delimiter','\n');
  s = s{1};
  s_e3t = s(strncmp('E3T',s,3));
  s_e4q = s(strncmp('E4Q',s,3));
  s_nd = s(strncmp('ND',s,2));
  
  [~,cell_num_t,node1_t,node2_t,node3_t,mat] = strread([s_e3t{:}],'%s %u %u %u %u %u');
  node4_t = node1_t;
  e3t = [node1_t,node2_t,node3_t,node4_t];
  [~,cell_num_q,node1_q,node2_q,node3_q,node_4_q,~] = strread([s_e4q{:}],'%s %u %u %u %u %u %u');
  e4q = [node1_q,node2_q,node3_q,node_4_q];
  [~,~,node_X,node_Y,~] = strread([s_nd{:}],'%s %u %f %f %f');
  
  cell_id = [cell_num_t;cell_num_q];
  [~,i] = sort(cell_id,1,'ascend');
  
  cell_node = [e3t;e4q];
  cell_node = cell_node(i,:);
  </code></pre>
  
  <p>Any help appreciated.
  Alex</p>