📄 modnum.c
字号:
/* 根据编号修改成员收支信息 */
#ifndef MODIFYBYNUMBER
#define MODIFYBYNUMBER
#include "stdio.h"
#include "shead.h"
void ModifyByNumber()
{
int i,j,k;
long modnum;/* 要修改的成员编号 */
long Number;
char Name[20];
float in;
float out;
float sum;
money TempS;
money *s=NULL,*t=NULL;
int total=FINANCESIZE;/* FINANCESIZE 在shead.h中定义 */
int recNumber;
char DataFile[10]="finance",next;
FILE *fp;
system("cls");
s=(money*)malloc(total*sizeof(money));
if(s==NULL)
{
printf("malloc memory fail!\n");
printf("press any key to end program!");
fflush(stdin);
getch();
exit(0);
}
begin:
/* 以只读的方式打开文件,如果不存在提示错误 */
fp=fopen(DataFile,"rb");
if(fp==NULL)
{
printf("open file %s fail!",DataFile);
perror("open file fail");
fflush(stdin);
getch();
free(s);/*释放内存*/
return;
}
printf("Please input the Employee's seatnum which you will modify: ");
scanf("%ld",&modnum);
printf("the finance you will delete is %ld\n",modnum);
/* 输入要修改的各项内容值 */
Number=modnum;
printf("Name:");
scanf("%s",Name);
Name[19]='0';
printf("in:");
scanf("%f",&in);
printf("out:");
scanf("%f",&out);
/* 自动计算财务合计 */
sum=in-out;
/* 将文件中要修改的信息存入结构体数组 */
recNumber=0;
/* 将文件中的数据读到动态分配的内存空间中 */
while(fread(&s[recNumber],sizeof(money),1,fp)!=(int)NULL)
{
if(s[recNumber].Number==modnum)
{
s[recNumber].Number=Number;
s[recNumber].in=in;
s[recNumber].out=out;
s[recNumber].sum=sum;
strcpy(s[recNumber].Name,Name);
}
recNumber++;
if(recNumber>=total)
{
total+=FINANCEINCREASE;
t=realloc(s,total*sizeof(money));
if(t==NULL)
{
printf("allot memory fails");
fflush(stdin);
getchar();
free(s);
exit(0);/* 内存分配失败退出 */
}
s=t;
t=NULL;
}
}
fclose(fp);
fp=fopen(DataFile,"wb+");
if(fp==NULL)
{
printf("Open file %s fial!end with any key.\n",DataFile);
perror("Open file fail");
fflush(stdin);
getch();
free(s);/*释放内存*/
return;
}
for(i=0;i<recNumber;i++)
{
if(fwrite(&s[i],sizeof(money),1,fp)!=1)
{
printf("Write 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.\n");
perror("Open file fail");
fflush(stdin);
getch();
free(s);/*释放内存*/
return;
}
printf("the file after modify 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%.3f\t\t%.3f\t\t%.4f",TempS.Number,TempS.Name,TempS.in,TempS.out,TempS.sum);
}
fclose(fp);
/* 提示是否进行下一次修改 */
printf("\nGo on(y/n):");
fflush(stdin);
next=getchar();
if(next=='y'||next=='Y')goto begin;
free(s);/*释放内存*/
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -