Hi, I am currently writing a program that requires me to take values from a text file and store them in an 2d array, I am quite new to c++ programming so I am not sure how to do it.

My text file (posvalues.txt) looks like this:

0 0 0
5 0 5
10 0 10
15 0 10
20 0 10

And I need those values in a 3x5 array. Any help would be very much appreciated.

Thanks

Welcome. What have you tried so far?

not much really as I am not too sure of the c++ file handling syntax currently I am just opening the file:

ifstream f;
f.open("posvalues.txt");

and I want to read through the file and input the values into an array:
float values[3][5];

What you have won't be too difficult. If you can write a for loop to iterate through your array, just use the >> operator to read an entry from your file into each slot. It will get the next value after a space or newline. Give it a try and then post back if you're still having trouble with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.