title: Reading 2d matrix from huge txt
body: <p>I have to write a function for a program that shou;d read a 2d array from a text file. The text file is variable, so i wrote functions that will calculate the numbers of rows and columns, I also wrote a function that displays the matrix, but I am having trouble with the function that should read the values from the txt file into the 2d array.
  I am pasting what I have done. Please keep in mind I have been looking online for an answer to my problem for 4 days.
  So what I need help with is the function that gets the values from the txt into the matrix. Thank you.</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;time.h&gt;
  #include &lt;fstream&gt;
  #include &lt;istream&gt;
  #include &lt;iomanip&gt;
  #include &lt;sstream&gt;
  #include &lt;string&gt;
  #include &lt;stdio.h&gt;
  using namespace std;
  
  /*Declaring unviversal variables*/
  
  ifstream datafile ("randaex1.txt");             /*Original data file*/
  ifstream testfile ("test1.txt");
  ofstream outputfile;                            /*Output     data file*/
  string line,a,b,auxiliar;
  stringstream ss;
  int number_of_lines,col,matrix[100][15000];
  int A[4][5] = {{1, 2, 3, 4, 5},
             {6, 7, 8, 9, 10},
             {11, 12, 13, 14, 15},
             {16, 17, 18, 19, 20}};
  
  
  
  int readfromfile()                              /*Test     function. Reads the first line of the file into variable "line"*/
  {
  if (datafile.is_open())
  {
      getline (datafile,line);
      datafile.close();
  }
  else
      cout &lt;&lt; "Unable to open file";
  return 0;
  }
  
  int writetofile ()                              /*Test     function. Writes "line" to the output file.*/
  {
  outputfile.open ("example.txt");
  outputfile &lt;&lt; line &lt;&lt; "\n";
  outputfile.close();
  cout &lt;&lt; "\nThe line\n \n" &lt;&lt; line &lt;&lt; "\n\nwas copied to example.txt" &lt;&lt; endl;           /*Gives feedback of the copied line to user.*/
  return 0;
  }
  
  int sizeofline()                            /*Test function.     Calculates the size of a line*/
  {
  cout &lt;&lt; "\nThe size of line is " &lt;&lt; line.size() &lt;&lt; " characters.\n";
  return line.size();
  }
  
  int linesinfile()                           /*Calculates the     number of lines in datafile(the number of rows in matrix)*/
  {
  number_of_lines=0;
  std::string liney;
  std::ifstream myfile("randaex1.txt");
  
  while (std::getline(myfile, liney))
  {
      ++number_of_lines;
  }
  
  std::cout &lt;&lt; "Number of lines in text file: " &lt;&lt; number_of_lines;
  
  system("pause");
  return number_of_lines;
  }
  
  
  int columns()                           /*Calculates the number of columns in datafile*/
  {
  
  col=0;
  for (int i=0;i&lt;=line.size();i++)
      if (line[i]==' ')
          col++;
  col = col++;
  cout &lt;&lt; col;
  return col;
  }
  
  void Readm(int N, int M)
  {
  for(int R=0;R&lt;N;R++)
  {
      for(int C=0;C&lt;M;C++)
          cin&gt;&gt;A[R][C];
      cout&lt;&lt;endl;
     }
  }
  
  
  
  void Displaym(int N, int M)
  {
  N=4, M=5;
  for(int R=0;R&lt;N;R++)
  {
      for(int C=0;C&lt;M;C++)
          cout&lt;&lt;setw(10)&lt;&lt;A[R][C];
      cout&lt;&lt;endl;
     }
  }
  
  void matrixfile()                       /*Reads file to matrix*/
  {
  string str[10][5];
  int a = 0;
  int b = 0;
  if(!testfile) //Always test the file open.
  {
              cout&lt;&lt;"Error opening output file"&lt;&lt;endl;
              system("pause");
  
  }
  while(!testfile.eof())
  {
    getline(testfile,str[a][b],' ');
    cout &lt;&lt; str[a][b];
    system("pause");
    if(a == 5)
    {
         a=0;
         ++b;
         getline(testfile,str[a][b],' ');
         cout &lt;&lt; str[a][b];
         system("pause");
    }
    a++;
      }
  b=0;
  for(int i=0; i&lt;=3; i++)    //This loops on the rows.
  {
      for(int j=0; j&lt;=4; j++) //This loops on the columns
      {
          cout &lt;&lt; str[i][j]  &lt;&lt; " ";
  
      }
      cout &lt;&lt; endl;
  }
  }
  
  
  
  int main()
  {
  readfromfile();
  writetofile();
  sizeofline();
  getchar();
  /*linesinfile();*/
  getchar();
  columns();
  getchar();
  Displaym(4,5);
  getchar();
  return 0;
  }
  </code></pre>