title: Read table of numbers into arrays when number of rows and columns determined at runtime
body: <p>I would like to ask you about data input. I have a text file in the following format: </p>
  
  <p>7 2 </p>
  
  <p>X       Y</p>
  
  <p>1   0<br>
  2   0.048922<br>
  3   0.0978829<br>
  4   0.146908<br>
  5   0.196019<br>
  6   0.245239<br>
  7   0.294584    </p>
  
  <p>The first line contains the number of rows and columns to be read in. The second line are headers. From the third line onwards it's only data. I would like to read my data into a 2D array (mat[][]) and the headers into an array of strings (title[]), which could be easily referenced later. I came this far writing the script. It can read the array numbers into the array, but not a 2D one. I was trying to declare a pointer for the matrix which is passed to the function but I couldn't do it. I also tried getline() but I do not know how to separate the headers \t delimited into separate strings.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;cmath&gt;
  using namespace std;
  
  void readFile(char[]);
  
  int main(){
  
      char fileName[] = "results.txt";
  
      readFile(fileName);
      return 0;
  }
  
  
  // Read file to matrix
  void readFile(char fileName[]){
  
      int m, n;
  
      // Create streamobject
      ifstream infile;
      infile.open(fileName);
  
      // Exit if file opening failed
      if (!infile.is_open()){
          cerr&lt;&lt;"Opening failed"&lt;&lt;endl;
          exit(1);
      }
  
      // Get size of the matrix
      infile &gt;&gt; m &gt;&gt; n;
  
      // Pre-allocate memory
      string title;
      float *mat=new float[m*n];
  
      // Read title
  
      // Start reading data
      while (!infile.eof()){
          for(int i=0; i&lt;m; i++){
              for(int j=0; j&lt;n; j++){
                  infile &gt;&gt; mat[i*n+j];
              }
          }
      }
  
      infile.close();
  }
  </code></pre>
  
  <p>Could anyone help me out?</p>
  
  <hr>
  
  <p>Thank you for the help so far. I will have a look at applying vectors this weekend, but I have to read up on it. I have quickly changed my script so now it does what I wanted: return a pointer to an array "mat" with the values. I have a function called 'showMatrix', which prints it to the screen. 
  If I call showMatrix within the function 'readFile' it works fine. If on the other hand I call it outside from the main script then it returns completely wrong values. </p>
  
  <p>I have a feeling there is something wrong here. Could you please help to point it out? </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;cmath&gt;
  using namespace std;
  
  void readFile(char[],float *mat,int &amp;m,int &amp;n);
  void showMatrix(float *mat,int m,int n);
  
  
  /*******************************************************************
   Script
   *******************************************************************/
  int main(){
  
      char fileName[] = "results.txt";
      int m, n;
      float *mat;
  
      // Read data from file
      readFile(fileName,mat,m,n);
      showMatrix(mat,m,n);
  
      return 0;
  }
  
  // Read file to matrix
  void readFile(char fileName[],float *mat,int &amp;m,int &amp;n){
  
  
      // Create streamobject
      ifstream infile;
      infile.open(fileName);
  
      // Exit if file opening failed
      if (!infile.is_open()){
          cerr&lt;&lt;"Opening failed"&lt;&lt;endl;
          exit(1);
      }
  
      // Get size of the matrix
      infile &gt;&gt; m &gt;&gt; n;
  
      // Pre-allocate memory
      mat=new float[m*n];
  
      // Read title
      std::string X;
      std::string Y;
      infile &gt;&gt; X &gt;&gt; Y;
  
      // Reading data
      while (!infile.eof()){
          for(int i=0; i&lt;m; i++){
              for(int j=0; j&lt;n; j++){
                  infile &gt;&gt; mat[i*n+j];
              }
          }
      }
  //    showMatrix(mat,m,n);
      infile.close();
  }
  
  // Print matrix to screen
  void showMatrix(float *x,int m, int n){
      for(int i=0; i&lt;m; i++){
          for(int j=0; j&lt;n; j++){
              cout &lt;&lt; x[i*n+j] &lt;&lt; "\t";
          }
          cout &lt;&lt; endl;
      }
  }
  </code></pre>