reading in data into an array of arrays
Hey,
I have 0 experience in C (although I know R somewhat well and know a little with Perl) and was assigned a completely unreasonable assignment in my R computing class. I'm trying to read in a text file into C as a multidimensional array. The data is 100 rows by 6 columns and contains positive and negative values with decimals (ie -1.24354). I want to store this data in C so that data[0][0] corresponds with the value in the first row and column.
Code:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#define nRow 100
#define nCol 6
int main()
{
int i,j;
double data[nRow][nCol];
FILE *fpIn = NULL;
fpIn = fopen("data.txt", "r");
if(fpIn == NULL)
{
printf("Cannot open file!\n");
getchar();
return 1;
}
else
{
printf("File is now open. Press Enter...\n");
}
for(i = 0; i < nRow; i++) {
for(j = 0; j < nCol; j++) {
fscanf(fpIn, "%d", &data[i][j]);
}
}
printf("file has been read in\n");
printf("the first entry is:\n");
printf("%d\n", &data[1][0]);
getchar();
fclose(fpIn);
return 0;
}
The value is supposed to be -1.12, but compiling and running the code returns: 2288848. When I change %d into %s, it reads the correct string, but I want these numbers as doubles. What exactly I am doing wrong?
Thanks in advance!