modnum.c
上传用户:tamljx
上传日期:2021-06-04
资源大小:60k
文件大小:4k
- /* modnum.c :修改家庭成员收支信息记录 */
- #include "stdio.h"
- void ModifyByNumber()
- {
- int i,j,k;
- long modnum;/*存储家长输入的要修改的家庭成员序号*/
- /*输入各项修改后信息*/
- long Number;
- char Name[20];
- float in;
- float out;
- float sum;
- money TmpS; /*定义进行操作时的临时结构体变量*/
- money s[SIZE];/*SIZE,在shead.h头文件中定义的常量,值为100 */
- int recNumber;
- char DataFile[40] = "",next;
- /*DataFile存储家庭成员收支信息信息的文件名,next为是否进行下一次删除操作的选项*/
- FILE *fp;/*====fp指针指向存储数据的文件名====*/
- /*提示家长输入要进行修改记录的文件名*/
- printf("nplease input the name of file where data is stored,end with enter key.n");
- gets(DataFile);
- /*提示家长输入要进行修改记录的文件名*/
- while(*DataFile == (' '))
- {
- printf("nplease input the name of file where data is stored,end with enter key.n");
- gets(DataFile);
- }
- begin:
- /*以读的方式打开文件,如文件不存在,提示错误*/
- fp=fopen(DataFile,"rb");
- if (fp == NULL)
- {
- printf("nOpen file %s fail!End with any keyn",DataFile);
- perror("Open file fail");
- getch();
- exit(1);
- }
- printf("please input the Employee'seatnum which you will modify:");
- scanf("%ld",&modnum);
- printf("the money you will delete is:%ldn",modnum);
- /*输入要修改记录的各项内容值*/
- Number=modnum;
- printf("name=");
- scanf("%s",Name);
- printf("in=");
- scanf("%f",&in);
- printf("out=");
- scanf("%f",&out);
- /*用公式自动计算家庭成员财务合计*/
- sum=in-out;
- /*将文件中要修改的信息存入结构体数组*/
- recNumber=0;
- /*循环将文件数据读入结构体数组,
- 如文件中的数据家庭成员号和要修改的家庭成员号不符,则原样写入数组,
- 如文件中数据的家庭成员号和要修改家庭成员号匹配,
- 则根据家长输入的各项修改内容重新赋值,即修改,并写入数组*/
- while((fread(&TmpS,sizeof(money),1,fp)) != (int)NULL)
- {
- if(TmpS.Number!=modnum)
- {
- s[recNumber].Number = TmpS.Number;
- strcpy(s[recNumber].Name, TmpS.Name);
- s[recNumber].in = TmpS.in;
- s[recNumber].out = TmpS.out;
- s[recNumber].sum = TmpS.sum;
- recNumber++;
- }
- else
- {
- s[recNumber].Number = Number;
- strcpy(s[recNumber].Name, Name);
- s[recNumber].in = in;
- s[recNumber].out = out;
- s[recNumber].sum = sum;
- recNumber++;
- }
- }
- fclose(fp);
- /*将修改后的结构体数组记录写入文件*/
- fp=fopen(DataFile,"wb+");
- if (fp == NULL)
- {
- printf("nSet up file %sfail !end with anykey.n",DataFile);
- perror("Set up fail");
- getch();
- exit(1);
- }
- for(i=0; i<recNumber; i++)
- {
- if(fwrite(&s[i],sizeof(money),1,fp)!=1)
- {
- printf("nWrite file %s fail!end with anykey.n",DataFile);
- perror("Write file fail!");
- getch();
- exit(1);
- }
- }
- fclose(fp);
- /*显示修改后的文件*/
- fp=fopen(DataFile,"rb");
- if (fp == NULL)
- {
- printf("nOpen file%sfail!End with any key n",DataFile);
- perror("Open file fail");
- getch();
- exit(1);
- }
- printf("the file after modify is:n");
- printf("nNumber tNametintouttsumn");
- while(fread(&TmpS,sizeof(money),1,fp) != (int)NULL)
- {
- if(TmpS.Number!=0)
- printf("n%ldt%st%4.1ft%4.1ft%4.1fn",TmpS.Number,TmpS.Name,TmpS.in,TmpS.out,TmpS.sum);
- }
- fclose(fp);
- /*提示是否进行下一次修改*/
- printf("nGo on ?(y/n)");
- next=getche();
- putchar('n');
- if ( next =='y' || next =='Y') goto begin;
- }