title: C++ program to read a csv matrix and print the transpose
body: <p>I currently have a C++ program to read in a matrix of values (type double) and print out the transpose, how can I alter my code so that it can read in csv files rather than spaced matrix text?</p>
  
  <p>I also need to write out the transposed matrix to another file after calculating it, what would be the simplest way of doing this?</p>
  
  <pre><code>int readMatrix(const string &amp; s, vector&lt;double&gt; &amp; v);
  void import_csv_matrix(const char* filename_matrix, vector &lt;double&gt; &amp; v, int&amp; rows, int&amp; columns);
  
  
  int main() {
      vector&lt;double&gt; v;
      int rows = 0;
      int columns = 0;
  
      import_csv_matrix("filename.csv", v, rows, columns);
      return 0;
  }
  
  int readMatrix (const string &amp; s, vector&lt;double&gt; &amp; v) {
      istringstream is(s);
      double n;
      while (is &gt;&gt; n) {
          v.push_back(n);
      }
      return v.size();
  }
  
  void import_csv_matrix(const char* filename_matrix, vector&lt;double&gt;&amp; v, int&amp; rows, int&amp; columns) {
  
      ifstream data_File;
      string line;
  
      data_File.open(filename_matrix);
      if (data_File.is_open()) {
          int i = 0;
          getline(data_File, line);
  
          columns = readMatrix(line, v);
  
          for (i = 1; i &lt; 100000; i++) {
              if ( !getline(data_File, line) &gt; 0) break;
              readMatrix(line, v);
          }
  
          rows = i;
  
          data_File.close();
      }
  
      cout &lt;&lt; "Transpose:" &lt;&lt; endl;
      for (int i = 0; i &lt; columns; i++) {
          for (int j = 0; j &lt; rows; j++) {
              cout &lt;&lt; v[i + j*columns] &lt;&lt; "\t";
          }
          cout &lt;&lt; endl;
      }
  }
  </code></pre>