struct chexinxi *duqu() { struct chexinxi *head2= NULL; struct chexinxi *temp = NULL; char chexinghao[20], chepaihao[20]; FILE *fp = fopen("车辆信息.txt", "r"); if (fp == NULL) { printf("\t\t\t\t打开文件失败!文件名:%s,错误码:%d\n", "车辆信息.txt", errno); } else { while (fscanf(fp, "%s %s", chexinghao, chepaihao) != EOF) { struct chexinxi *p = malloc(sizeof(struct chexinxi)); if (p == NULL) { printf("分配内存失败!\n"); } strcpy(p->chexinghao, chexinghao); strcpy(p->chepaihao, chepaihao); if (head2 == NULL) { head2 = temp = p; } else { temp->next = p; temp = p; } } fclose(fp); if (temp != NULL) { temp->next = NULL; } return head2; } return; }是否有错
时间: 2024-01-19 09:04:46 浏览: 64
内存错误
这段代码在处理文件打开失败的情况时进行了错误处理,但是在文件打开成功时缺少了返回指针的语句,导致程序会进入到无返回值的分支并且出现编译错误。因此,需要在文件处理完成后添加返回指针的语句,如下所示:
```
struct chexinxi *duqu() {
struct chexinxi *head2= NULL;
struct chexinxi *temp = NULL;
char chexinghao[20], chepaihao[20];
FILE *fp = fopen("车辆信息.txt", "r");
if (fp == NULL) {
printf("\t\t\t\t打开文件失败!文件名:%s,错误码:%d\n", "车辆信息.txt", errno);
return NULL; // 文件打开失败,返回空指针
} else {
while (fscanf(fp, "%s %s", chexinghao, chepaihao) != EOF) {
struct chexinxi *p = malloc(sizeof(struct chexinxi));
if (p == NULL) {
printf("分配内存失败!\n");
}
strcpy(p->chexinghao, chexinghao);
strcpy(p->chepaihao, chepaihao);
if (head2 == NULL) {
head2 = temp = p;
} else {
temp->next = p;
temp = p;
}
}
fclose(fp);
if (temp != NULL) {
temp->next = NULL;
}
return head2; // 文件处理完成,返回链表头指针
}
}
```
阅读全文