Read size of data array when reading .txt data
Hi a newbie here (just started learning C two weeks ago). I like C as a fresh start. I have been using R and Matlab for most numerical analysis. But it takes a lot of times and CPU when I use a massive data. So that motivated me to take advantage of comiplier-based languages instead of interpreter-based ones.
Anyways, I would like to know how to let a program read the size of data array imported, and how to store values at the jth row and at the kth column into an j*k array, in C.
I tried the follwing codes to start with. I type the file name and let this program to read the file.. But it did not work. The values in the file are xx.xxx (double).
Code:
#include<stdio.h>
int main()
{
/* define variables */
FILE *fp;
double read_arr[6][2];
char filename[80];
int j=0,k=0;
/* ask and get a file name to be read */
printf("Filename=: ");
gets(filename);
/* Show error message when the file doesn't exist in the current directory */
if((fp=fopen(filename,"r"))== NULL)
{
printf("Cannot open file\n");
exit(1);
while(!feof(fp))
{
for(j=0;j<10;j++)
for(k=0;k<2;k++)
{
fscanf(fp,"%f",&(read_arr[i][j]));
printf("%f",read_arr[i][j]);
}
printf("\n");
}
fclose(fp);
}}
I am not sure why this does not work. No compiling errors, but no printout either.
So the first question is in what part I did wrong. and the second is that instead of assigning 6 and 2 in read_arr[6][2] how can I let the program determine the size of array? I have many array data whose sizes are not the same. Eventually, i would like to loop this process and save the results, while I am doing something else.