deluser.c
上传用户:tamljx
上传日期:2021-06-04
资源大小:60k
文件大小:2k
- /* deluser.c 删除用户信息 */
- #include "stdio.h"
- void DelUser()
- {
- int i,j,k;
- long delnum;
- 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 delete:");
- scanf("%ld",&delnum);
- printf("the user you will delete is:%ldn",delnum);
- recNumber=0;
- while((fread(&TmpS,sizeof(user),1,fp)) != (int)NULL)
- {
- if(TmpS.Number!=delnum)
- {
- s[recNumber].Number = TmpS.Number;
- strcpy(s[recNumber].Name, TmpS.Name);
- strcpy(s[recNumber].ps,TmpS.ps);
- s[recNumber].power = TmpS.power;
- recNumber++;
- }
- }
- printf("end shuzu while");
- 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 delete is:n");
- printf("nNumber ttNametpstpowern");
- 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;
- }