📄 delete.c
字号:
/* 按编号删除家庭成员收支信息 */
#ifndef DELETE
#define DELETE
#include <stdio.h>
#include <malloc.h>
#include "shead.h"
void DelRecord()
{
int i,j,k;
long delnum;/* 存放要删除的记录编号 */
money TempS;
money *s=NULL,*t=NULL;
int total=FINANCESIZE;/* FINANCESIZE在shead头文件中定义为50 */
int recNumber;
char DataFile[10]="Finance",next;
FILE * fp;
s=(money *)malloc(sizeof(money)*total);
if(s==NULL)
{
printf("malloc memory fail!\n");
printf("Press any key to end!");
fflush(stdin);
getch();
}
system("cls");
begin:
fp=fopen(DataFile,"rb");
if(fp==NULL)
{
perror("Open file fail");
fflush(stdin);
free(s);/*释放内存空间*/
getch();
return;
}
printf("please input the employee's seatnum which you will delete:");
scanf("%ld",&delnum);
printf("the money you will delete is:%ld\n",delnum);
/* 将文件信息存入结构体堆空间 */
/* 与要删除的家庭成员序号相匹配的项不写入存储空间 */
recNumber=0;
while(fread(&TempS,sizeof(money),1,fp)!=(int)NULL)
{
if(TempS.Number==delnum)
{
printf("Number:%ld Name:%s\n",TempS.Number,TempS.Name);
printf("Do you want to 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].in=TempS.in;
s[recNumber].out=TempS.out;
s[recNumber].sum=TempS.sum;
recNumber++;
if(total<=recNumber)
{
total+=FINANCEINCREASE;
t=(money*)realloc(s,total*sizeof(money));
if(t==NULL)
{
printf("malloc memory fail");
fflush(stdin);
getch();
free(s);/*释放内存空间*/
}
s=t;
t=NULL;
}
}
fclose(fp);
fp=fopen(DataFile,"wb+");
if(fp==NULL)
{
perror("Open file fail");
fflush(stdin);
free(s);/*释放内存空间*/
getch();
return;
}
for(i=0;i<recNumber;i++)
{
if(fwrite(&s[i],sizeof(money),1,fp)!=1)
{
printf("\nWrite file %s fail!\n",DataFile);
perror("write file fail");
fflush(stdin);
getch();
free(s);/*释放内存*/
return;
}
}
fclose(fp);
/* 显示删除后的文件 */
fp=fopen(DataFile,"rb");
if(fp==NULL)
{
printf("\nWrite file %s fail!\n",DataFile);
perror("write file fail");
fflush(stdin);
getch();
free(s);/*释放内存*/
return;
}
printf("the file after delete is:\n");
printf("\nNumber\tName\t\tin\t\tout\t\tsum\n");
while(fread(&TempS,sizeof(money),1,fp)!=(int)NULL)
{
if(TempS.Number!=0)
printf("\n%ld\t%s\t\t%.2f\t\t%.2f\t\t%.2f\n",TempS.Number,TempS.Name,TempS.in,TempS.out,TempS.sum);
}
fclose(fp);
printf("go on(y/n):");
next=getchar();
putchar('\n');
if(next=='y'||next=='Y')goto begin;
free(s);/*释放内存*/
getch();
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -