void initializeCar(Car* v,char* mainFileIn)
{
int counter = 0;
int i = 0;
FILE* fp = fopen(mainFileIn, "r");
char* line = NULL;
size_t len = 0;
ssize_t read;
char* pch;
while((read = getline(&line, &len,fp)) != -1)
{
if(i == 50)
{
printf("Cannot hold more than 50 Cars.");
exit(1);
}
pch = strtok(line,",");
v[i].pid = ++counter;
v[i].xLocation = (float)strtod(pch,NULL);
printf("pch: %s\n",pch);
pch=strtok(NULL,",");
printf("pch: %s\n",pch);
v[i].yLocation = (float)strtod(pch,NULL);
pch = strtok(NULL,",");
printf("pch: %s\n",pch);
v[i].velocity = (float)strtod(pch,NULL);
pch = strtok(NULL,",");
printf("pch: %s\n",pch);
v[i].angle = (int)strtol(pch,NULL, 10);
v[i].start = NULL;
i++;
}
free(line);
fclose(fp);
}
由于某些原因,我的代码中的所有内容除了在我的最后一行之后读取一行外,还给了我一个分段错误。我无法弄清楚为什么,因为我根据getLine
教程中的一个while
循环来建模http://man7.org/linux/man-pages/man3/getline.3.html为什么我的getLine在这个c函数中走得太远了?
您是否在阅读完之后尝试打印该行,以确保获得了您认为正在获取的数据? – 2014-10-18 05:51:18