📄 delete.c
字号:
#ifndef DELETE
#define DELETE
#include "stdio.h"
#include "shead.h"
#include "malloc.h"
void DelRecord()
{
int i,j,k;
long delnum;/* 存放要删节除的商品编号 */
product TempS;
product *s,*t;
int total=PRODUCTSIZE;/* PRODUCTSIZE在头文件中定义 */
int recNumber;/* 原文件中的记录数 */
char DataFile[10]="product",next;
FILE * fp=NULL;
s=(product*)malloc(total*sizeof(product));
if(s==NULL)
{
printf("malloc memory fail!End with any key.\n");
fflush(stdin);
getch();
exit(0);
}
clrscr();
begin:
fp=fopen(DataFile,"rb");
if(fp==NULL)
{
printf("\nOpen file %s fail!End with any key\n",DataFile);
perror("Open file fail");
fflush(stdin);
free(s);/* 释放内存空间 */
getch();
return;
}
/* 输入要删除的商品序号 */
printf("Please input the product's seatnum withc you want delete:");
scanf("%ld",&delnum);
printf("the product you will delete is%ld\n",delnum);
recNumber=0;
while(fread(&TempS,sizeof(product),1,fp)!=(int)NULL)
{
if(TempS.Number==delnum)
{
printf("\nNumber\tName\tPrice\t\tDiscount\t\tmemberPrice\n");
printf(" %ld\t%.2s\t%.2f\t\t%-8.2f\t\t%.2f\n",TempS.Number,TempS.Name,TempS.price,TempS.discount,TempS.memberprice);
printf("Do you want delete it(Y/N)?");
fflush(stdin);
next=getchar();
if(next=='y'||next=='Y')
continue;
}
s[recNumber].Number=TempS.Number;
strcpy(s[recNumber].Name,TempS.Name);
s[recNumber].price=TempS.price;
s[recNumber].discount=TempS.discount;
s[recNumber].memberprice=TempS.memberprice;
recNumber++;
if(recNumber>=total)
{
total+=PRODUCTINCREASE;
t=(product*)realloc(s,total*sizeof(product));
if(t==NULL)
{
printf("allot memory fail!\n");
fflush(stdin);
getch();
free(s);
exit(0);
}
s=t;
t=NULL;
}
}
fclose(fp);
/* 将删除后的剩余记录写入文工团件 */
fp=fopen(DataFile,"wb+");
if(fp==NULL)
{
printf("\nOpen file fail!End with any key.\n");
perror("Open file fail");
fflush(stdin);
free(s);/* 释放内存空间 */
return;
}
for(i=0;i<recNumber;i++)
{
if(fwrite(&s[recNumber],sizeof(product),1,fp)!=1)
{
printf("\nWrite file %s fail!End with any key.\n",DataFile);
perror("Write File fail");
fflush(stdin);
getch();
free(s);/* 释放内存空间 */
return;
}
}
fclose(fp);
/* 显示删除后的文件 */
fp=fopen(DataFile,"rb");
if(fp==NULL)
{
printf("\nOpen file %s fail!End with any key",DataFile);
perror("Open file fail");
fflush(stdin);
getch();
free(s);/* 释放内存空间 */
return;
}
printf("the file after delete is:\n");
printf("\nNumber\tName\t\tprice\t\tdiscount\t\tmemberprice\n");
while(fread(&TempS,sizeof(product),1,fp)!=(int)NULL)
{
if(TempS.Number!=0)
{
printf("%ld\t%s\t\t%.2f\t\t%-8.2f\t\t%.2f\n",TempS.Number,TempS.Name,TempS.price,TempS.discount,TempS.memberprice);
}
}
fclose(fp);
printf("Go on(Y/N)?");
fflush(stdin);
next=getchar();
putchar('\n');
if(next=='Y'||next=='y') goto begin;
free(s);/* 释放内存空间 */
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -