struct chexinxi *shanchu() { FILE *fp; char str1[10]; char str2[10]; struct chexinxi *p,*pr,*head2,*q; p=head2=duqu(); if(head2==NULL) { printf("\t\t\t\t无内容!\n"); return NULL; } printf("\t\t\t\t请输入要删除的记录内容:\n"); printf("\t\t\t\t车型号:"); scanf("%s",str1); printf("\t\t\t\t车牌号:"); scanf("%s",str2); while(strcmp(str1,p->chexinghao)!=0&&strcmp(str2,p->chepaihao)!=0&&p->next!=NULL) { pr=p; p=p->next; } if(strcmp(p->chexinghao,str1)==0&&strcmp(str2,p->chepaihao)==0) { if(p==head) head2=p->next; else pr->next=p->next; free(p); fp=fopen("车辆信息.txt","w"); q=head2; while(q!=NULL) { fprintf(fp,"%s %s\n", q->chexinghao, q->chepaihao); q=q->next; } fclose(fp); } else printf("\t\t\t\t记录为空!\n"); }为什么不对
时间: 2024-02-24 10:55:32 浏览: 30
这段代码中存在几个问题:
1. 函数签名缺少返回类型:应该在函数名前面添加返回类型,例如 `void`。
2. 变量未定义:变量 `head` 在代码中未定义,应该改为 `head2`。
3. 循环条件不正确:在 `while` 循环中,应该使用逻辑运算符 `&&` 连接两个比较表达式,而不是 `||`。
4. 删除链表节点时出现问题:在找到目标节点后,应该先将其从链表中删除,再释放其内存。
5. 文件操作问题:在打开文件时,应该使用 `"a"` 模式打开文件以进行追加,而不是 `"w"` 模式覆盖文件。此外,在文件操作后,应该检查文件是否成功打开,以确保操作成功。
下面是一份修改后的代码,你可以参考一下:
```c
void shanchu() {
FILE *fp;
char str1[10];
char str2[10];
struct chexinxi *p, *pr, *head2, *q;
p = head2 = duqu();
if (head2 == NULL) {
printf("\t\t\t\t无内容!\n");
return;
}
printf("\t\t\t\t请输入要删除的记录内容:\n");
printf("\t\t\t\t车型号:");
scanf("%s", str1);
printf("\t\t\t\t车牌号:");
scanf("%s", str2);
while (strcmp(str1, p->chexinghao) != 0 && strcmp(str2, p->chepaihao) != 0 && p->next != NULL) {
pr = p;
p = p->next;
}
if (strcmp(p->chexinghao, str1) == 0 && strcmp(p->chepaihao, str2) == 0) {
if (p == head2)
head2 = p->next;
else
pr->next = p->next;
free(p);
fp = fopen("车辆信息.txt", "a");
if (fp == NULL) {
printf("\t\t\t\t打开文件失败!文件名: 车辆信息.txt\n");
return;
}
q = head2;
while (q != NULL) {
fprintf(fp, "%s %s\n", q->chexinghao, q->chepaihao);
q = q->next;
}
fclose(fp);
}
else
printf("\t\t\t\t记录不存在!\n");
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)