Reputation: 73
I have a text file and it this file I have something like this:
0.003 0.4 6
0.004 0.002 54
0.007 0.001 6
I need to read these numbers from text file and save them in a array or list like follow:
0.003 0.4 6
0.004 0.002 54
0.007 0.001 6
For Example if the Array name is myArray and I need to have some thing like this
myArray[0,0,0]=0.003
I found some methods which can read double from a text file but in all of them, the array will be like this
myArray=[0.003;0.4;6;0.004,0.002;54;....]
what should I do? Please help me, I am a beginner in programming.
Upvotes: 0
Views: 171
Reputation:
your program will read one number in each loop round .. so make two counters one for rows and the other one for columns, and increment columns counter each time u read one number, till u exceed number 3 .. then reset it to 0 and increment the row counter .. check out this code:
double array[3][3];
int i = 0, j = 0;
double num;
num = inFile.readline();
while ( !inFile.eof() )
{
array[i][j] = num;
if ( j == 2 ) // check if its last element in row i
{
i++; // move to row i+1 in array
j = 0; // reset index to first element in row
}
else
j++; // move to next element in array
num = inFile.readline();
}
Upvotes: 1