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.
So the file is formatted like this:
There are two numbers,
n
andm
, where1 <= n, m <= 20
,
Now n
and m
come in the file formatted like so: n m
(there is a space between the two numbers)
Now after that line there are n
lines of integers with m
elements each. So for example an input is like so: (The numbers are in the range) 0 <= # <= 50
5 3
2 2 15
6 3 12
7 3 2
2 3 5
3 6 2
So from this the program knows there are 15 elements, and can be held in an array like so:
int foo[5][3]
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)
2 2
9 2
6 5
3 4
1 2 29
9 6 18
7 50 12
How do I read this kind of input from a file in C++?