It would be nice if anyone could help me with my ‘program’. I am trying to read csv file and move it to 2D array. It stops on 17th line(out of 200).
int main ()
{
FILE * pFile;
double **tab;
char bufor [100];
int i=0;
tab = (double**)malloc(sizeof(double*));
pFile = fopen ("sygnal1.csv" , "r");
if (pFile == NULL) printf("Error");
else
while (fgets (bufor , 100 , pFile))
{
tab[i] = (double *) malloc(2 * sizeof(double));
sscanf(bufor, "%lf, %lf,", &tab[i][0], &tab[i][1]);
printf("%lf.%lf.\n",tab[i][0],tab[i][1]); //It's here only for testing
i++;
}
printf("number of lines read %d\n",i);
fclose (pFile);
system("PAUSE");
return 0;
}
You haven’t completely allocated memory for
tabyet. You’ve just allocated one (uninitialised) pointer. When i > 0 you’re into Undefined Behaviour. You need to allocate at least as many elements as there might be lines in your file, e.g.or use
reallocafter each iteration to increase the number of elements.