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