munum.c
上传用户:tamljx
上传日期:2021-06-04
资源大小:60k
文件大小:3k
- /* munum.c按用户序号修改用户信息 */
- #include "stdio.h"
- void ModifyByUserNumber()
- {
- int i,j,k;
- long modnum;
- /*输入各项修改后信息*/
- long Number;
- char Name[20];
- char ps[8];
- int power;
- user TmpS;
- user s[SIZE];/*SIZE,在user.h头文件中定义的常量,值为100 */
- int recNumber;
- char DataFile[40] = "yonghu",next;
- FILE *fp;/*====fp指针指向存储数据的文件名====*/
- 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 user'seatnum which you will modify:");
- scanf("%ld",&modnum);
- printf("the user you will delete is:%ldn",modnum);
- /*输入要修改纪录的信息*/
- Number=modnum;
- printf("name=");
- scanf("%s",Name);
- printf("ps=");
- scanf("%s",ps);
- printf("power=");
- scanf("%d",&power);
- /*将文件中要修改的信息存入结构体数组*/
- recNumber=0;
- while((fread(&TmpS,sizeof(user),1,fp)) != (int)NULL)
- {
- if(TmpS.Number!=modnum)
- {
- s[recNumber].Number = TmpS.Number;
- strcpy(s[recNumber].Name, TmpS.Name);
- strcpy(s[recNumber].ps,TmpS.ps);
- s[recNumber].power = TmpS.power;
- recNumber++;
- }
- else
- {
- s[recNumber].Number = Number;
- strcpy(s[recNumber].Name, Name);
- strcpy(s[recNumber].ps,ps);
- s[recNumber].power = power;
- recNumber++;
- }
- }
- printf("nrecNumber%dn",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(user),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("nNumbertNametpstpowern");
- while(fread(&TmpS,sizeof(user),1,fp) != (int)NULL)
- {
- if(TmpS.Number!=0)
- printf("n%ldt%st%st%dn",TmpS.Number,TmpS.Name,TmpS.ps,TmpS.power);
- }
- fclose(fp);
- /*是否继续修改*/
- printf("nGo on ?(y/n)");
- next=getche();
- putchar('n');
- if ( next =='y' || next =='Y') goto begin;
- }