So I'm trying to inport a file into an array. The file is of known size, containing 9 numbers (ie a 3x3 matrix) separated by spaces and new lines. I need to take this file, and turn it into an array. So far, I have the matrix.dat that looks like:
Code:
4 -5 3
6 -9 3
4 8 -1
and I'm trying to import this using nestled for loops like:#
Code:
input = fopen("matrix.dat", "r");
if(input == (FILE*) NULL)
{
printf("Can not open file matrix.dat\n");
exit(EXIT_FAILURE);
}
else
{
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
fscanf(input, "%f", &matrix[i][j]);
}
}
printf("%s", matrix);
}
exit(EXIT_SUCCESS);
Now i'm assuming one of two parts is the problem. Either my for loops are inproperly set up, or (as I am assuming) the 'fscanf' is wrong. The problem is, I'm not sure how to fscanf for multiple dimensional arrays. Any help?
Cheers