I am trying to read a csv file and store it into a 2D array, However it give me "segmentation fault" each time I run it.
Here is my code
#include <stdlib.h>
int main(void)
{
const char filename[] = "a.txt";
FILE *file = fopen(filename, "r");
if ( file != NULL )
{
char *line;
int i = 0, j=0, read=0;
double array [2][10];
line= malloc (80 * sizeof (char));
while ( i < 2 )
{
if ( fgets(line, sizeof line, file) == NULL )
{
break;
}
for (j=0;j <20 ; j++){
sscanf(line, "%lf,",&array[i][j],&read );
//printf("%d ",read);
line+=read;
}
++i;
}
fclose(file);
else
{
perror(filename);
}
return 0;
}
Here is my a.txt
301.0,36,0.285,2.88,15.000,301.0,36,0.285,2.88,15.000,301.0,36,0.285,2.88,15.000,301,36,0.285,2.88,15.000
302,88,0.247,2.88,75.500,301,36,0.285,2.88,15.000,301,36,0.285,2.88,15.000,301,36,0.285,2.88,15.000
Moreover how can change the first 'While' loop to make it more sufficient, so instead of '2' it should predicet number of lines !
Thank you in advance.