I've just started learning C++ and I need to find a way to read data from .txt file into an array of int. I've been browsing for quite some time, and I couldn't find any similar threads (maybe because of my bad English).
Lets say my .txt looks like this:
1 2
5 6 5 2 3 4 6
12 9 3 2 4 5 8 6
I need to save the first line into an array A and the second line into B when the number of elements in one line is unknown.
Is there an easy way to do that? Is there a way to find out how many numbers I have in one line?
There is no easy way when dealing with arrays directly. That is why there is the std::vector class -- which does all the nasty bookkeeping for you.
If you must stick to arrays, you have two options:
1. dynamically allocate the array, and resize it if you run out of space when reading the line
2. read the line once just to count the number of items, allocate your array, and read the line again to fill it
The same way you would when reading the line into the array, except that you don't save the things you read into the array. Keep in mind that this means you will have to seekg() on your input file to get back to the beginning of the line.