There are several ways you could do this, all of which start with opening the file first though. To do this, you will need to include fstream and create a "file in" reader (look at the ifstream
class).
Once you have that it's pretty straight forward. You can either grab character by character by just looping to the end of the file and storing the number accordingly OR you can grab the file line by line and then loop through each line to grab the numbers. If you do the later, there are methods that will help split the string into an array based on the spaces but I'll let you look that up. Also, getting the line will mean you will need to catch the newline character into a junk variable so it can continue on, else the next time you get a line it will just get that variable anyways.
for
and while
loops are going to be your friend in this.
Also, don't forget to close your file stream when you're done too.
EDIT:
I forgot to mention that if you do the line by line way, you will need to use some method to turn a string/array into an integer (atoi
is one, i also think there is a stoi
but you may need to include string
to use that or I'm mistaken about that one). Doing it character by character you can go straight into an integer, but you better make sure there isn't any non-number character in your file and that there won't be.