I just can’t figure out how to do a malloc. The following code just types the first 5 lines and then stops, any help would be appreciated!
// Read query points from query file------------------------------
double **queryPoint;
token=(char*)malloc(40);
int qp_count=0;
i=0;
qp_count=0;
while(fgets(line,sizeof(line),queryFile)!=NULL)
{
queryPoint=(double*)malloc(sizeof(double**));
queryPoint[qp_count]=(double*)malloc(sizeof(double*)*2);
printf("line:%s",line);
token = strtok(line," ,\t");
queryPoint[qp_count][0]=atof(token);
token = strtok(NULL, " ,\t");
printf("l[%d]=%lf \n",qp_count,queryPoint[qp_count][0]);
queryPoint[qp_count][1]=atof(token);
token = strtok(NULL, " ,\t");
printf("l[%d]=%lf \n",qp_count,queryPoint[qp_count][1]);
qp_count++;
}
{
This is the form of the query file
9.85797 5.72533
9.58711 2.09899
2.28203 7.19344
4.49096 5.50094
6.05297 1.60751
6.19901 1.52312
}
…
30 lines total
Initialize at the beginning:
For every line call:
Note that you may use realloc to resize the space for lines. Also note that you should add error handling.
To free the memory you need to call: