【发布时间】:2018-05-01 22:43:06
【问题描述】:
我正在为文件名传递 argv[1] 并且我遇到了段错误,但我不知道为什么。我正在使用链表来保存包含 2 个字符和一个 int 的汽车结构的实例。我觉得问题出在我的 fscanf 语句中,但我不确定。
void readFile(List *north, List *east, List *south, List *west, char *fileName) {
char *file = fileName;
FILE *fp = fopen(file, "r");
if(!fp) {
printf("File could not be opened...");
return;
}
while(!feof(fp)) {
Car *temp = NULL; //Initialize a blank car
fscanf(fp, "%s %s %d", &temp->direc, &temp->turn, &temp->time); //scan the direction path and time
//into the Car Node
//Add to North List
if(temp->direc == 'N') {
insertBack(north, temp);
}
//Add to East List
if(temp->direc == 'E') {
insertBack(east, temp);
}
//Add to South List
if(temp->direc == 'S') {
insertBack(south, temp);
}
//Add to West List
if(temp->direc == 'W') {
insertBack(west, temp);
}
else {
printf("Something went wrong...");
return;
}
}
fclose(fp);
}
【问题讨论】:
-
请使用所有警告和调试信息进行编译:
gcc -Wall -Wextra -g然后使用调试器gdb -
我使用的是 Mac,但无法正常工作
-
你应该可以在终端中使用
gdb。
标签: c file io segmentation-fault