📄 删除.cpp
字号:
#include "1.h"
void del()
{ void store();
FILE *fp;
int count,i,flag=0;
char del_name[20],yes;
buy_list p,d,head;
if((fp=fopen("store.txt","r"))==NULL)
{printf("\n不能打开文件!\n");
exit(0);
}
fread(&count,sizeof(int),1,fp);
p=d=(buy_list1 *)malloc(sizeof(buy_list1));
head=NULL;
for(i=0;i<count;i++)
{if(i==0) head=p;
else p->next=d;
p=d;
d=(buy_list1 *)malloc(sizeof(buy_list1));
fread(d,sizeof(buy_list1),1,fp);
}
fclose(fp);
p->next=d;
d->next=NULL;
p=head;
/*while(p)
{printf("%s ",p->name);
p=p->next;} */
printf("输入你要删除的书名,出版社,或者作者\n");
scanf("%s",del_name);getchar();
while(p&&p->next)//p当删除的是最后一个记录时用的.
{ if(!strcmp(p->next->name,del_name)||!strcmp(p->next->author,del_name)||!strcmp(p->next->press,del_name)){
flag=1;//查找到
printf("书名:%-15s 作者:%-15s 出版社:%-15s \n",p->next->name,p->next->author,p->next->press);
printf("以上的书本是否你要删除的(Y/N)?");
yes=getchar();
if(yes=='y'||yes=='Y')
{count--;//节点数减1
d=p->next;
p->next=d->next;
free(d);
}// if(yes=='y'||yes=='Y')
}//if
p=p->next;}//while(p)
if(!flag) printf("找不到你所输入的!\n");
p=head->next;
fp=fopen("store.txt","w");
fwrite(&count,sizeof(int),1,fp);//写节点的数目
for(i=0;i<count;i++){
fwrite(p,sizeof(buy_list1),1,fp);
p=p->next;
}
fclose(fp);
store();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -