title: Reading certain lines into an 2D array with C++
body: <p>Okay, I was going through a few programming exercises, and got stuck with one involving reading a file. What I need to do is read in a certain set of lines into a 2D array, the lines length and amount of line varies, but I know it beforehand.</p>
  
  <p>So the file is formatted like this:</p>
  
  <blockquote>
    <p>There are two numbers, <code>n</code> and <code>m</code>, where <code>1 &lt;= n, m &lt;= 20</code>, </p>
  </blockquote>
  
  <p>Now <code>n</code> and <code>m</code> come in the file formatted like so: <code>n m</code> (there is a space between the two numbers)</p>
  
  <p>Now after that line there are <code>n</code> lines of integers with <code>m</code> elements each. So for example an input is like so: (The numbers are in the range) <code>0 &lt;= # &lt;= 50</code></p>
  
  <pre><code>5 3
  2 2 15
  6 3 12
  7 3 2
  2 3 5
  3 6 2
  </code></pre>
  
  <p>So from this the program knows there are 15 elements, and can be held in an array like so:
  <code>int foo[5][3]</code></p>
  
  <p>So how do I read in the file like this? And, lastly, the file has multiple sets of input after one another. So it might go: (2, 2 is info for first set, and 3, 4 for the second set of inputs)</p>
  
  <pre><code>2 2
  9 2
  6 5
  3 4
  1 2 29
  9 6 18
  7 50 12
  </code></pre>
  
  <p>How do I read this kind of input from a file in C++?</p>