title: C++: How to read large txt file and save to array faster
body: <p>I want to read a large txt file which has more than 50000 lines.</p>
  
  <p>sample of the files:</p>
  
  <pre><code>John 12 1 956 02 818 912 322 43 161 9 002 768 23 79 9 1 115 7 2 18 59 58 989 3 56 82 59 147 86 62 06 10 538 36 694 952 71 0 2 5 67 103 6 295 933 428 9 70 708 6 73 449 57 283 6 48 139 5 140 34 5 9 95 74 892 9 387 172 44 05 67 534 7 79 5 565 417 252 480 22 503 089 76 433 93 36 374 97 035 70 2 896 0 3 0 259 93 92 47 860
  </code></pre>
  
  <p><strong>description:</strong> 
  Above sample is each line in the txt file. each character and string are divided by space.</p>
  
  <p><strong>The goal:</strong> 
  I want to save the value of integer after the first word (in this case: John) and save to a Intager Matrice whose row = <strong>number of line in the txt file</strong> and column = 100.</p>
  
  <p><strong>Here is my code</strong></p>
  
  <pre><code>Mat readInteger(String path_txt_file){  
  int row = 1;
  int col = 100;
  Mat return_mat;
  
  Mat tmp_mat= Mat(row, col, CV_32F);
  
  fstream input(path_txt_file);
  for (std::string line; getline(input, line);)
  {
      int posMat = -1;
      vector&lt;string&gt; v = split&lt;string&gt;(line, " ");
      for (int i = 1; i &lt; v.size(); i++)
      {   
          posMat = posMat + 1;
          tmp_mat.at&lt;float&gt;(0, posMat) = atoi(v[i].c_str()); //white  
      }
      return_mat.push_back(tmp_mat);
  }
  tmp_mat.release();
  return return_mat;
  }
  </code></pre>
  
  <p><strong>Code description</strong></p>
  
  <ol>
  <li>I followed the classical way of reading the data from txt file is
  read line by line</li>
  <li>I created two Mat, return_mat and tmp_mat </li>
  <li>Each time for each line
  tmp_mat whose row = 1 and col = 100 is used to store integer each line 
  then we split string according to whitespace
  after that we push the whole tmp_mat to return_mat.</li>
  </ol>
  
  <p><strong>Result</strong>
  I got the result I want; <strong>unfortunately</strong>, when the file is too big (and we need that). The process is too slow.</p>
  
  <p><strong>Question</strong></p>
  
  <p><strong>How can we improve this algo. to deal with large file 1000000 lines for instance?
  I wonder if we should use multithreading?</strong></p>
  
  <p>Thank</p>