⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 deluser.c

📁 商品管理系统 1)进货员对商品基本信息进行输入、删除、修改和查询 (2)销售员对商品的基本信息进行查询和统计 (3)并可以对库存信息进行查询和修改 (4)顾客购买商品
💻 C
字号:
#include "stdio.h"
#include "user.h"
#include "malloc.h"
/* 删除用户 */
void DelUser()
{
 int i,j,k;
 long delnum;
 user TempS;
 user *t=NULL,*s=NULL;
 int total=USERSIZE;/*存放S的大小*/
 int recNumber;
 char DataFile[10]="yonghu",next;
 FILE *fp;/* fp指针指向存储数据的文件名 */
 clrscr();
 begin:
   fp=fopen(DataFile,"rb");
   if(fp==NULL)
   {
    printf("\nOpen file %s fail! End with any key \n",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:%ld\n",delnum);
   if(delnum==-1)
   {
    printf("Input Number error!\nPress any key to continu...");
    getch();
    return;
   }
   recNumber=0;
   /* 读文件到数组中,并根据删除编号完成删除功能 */
   if(s==NULL)
   {
      s=(user*)malloc(USERSIZE*sizeof(user));/* SIZE在user.h头文件中定义的常量,值为100 */
      if(s==NULL)
      {
         printf("Allot memory fail!");
         printf("press any key to continu...");
         getch();
         return;
      }
   }
   while((fread(&TempS,sizeof(user),1,fp))!=(int)NULL)
   {
    if(TempS.Number==delnum) /* 如果是要删除的用户则询问是否删除 */
    {
     printf("\nNumber\tName\tPower\n");
     printf("%ld\t%s\t%d\n",TempS.Number,TempS.Name,TempS.power);
     printf("Are you sure to delete this user(y/n):");
     fflush(stdin);
     next=getchar();
     if (next=='y'||next=='Y')
     {
      continue;
     }
    }
     s[recNumber].Number=TempS.Number;
     strcpy(s[recNumber].Name,TempS.Name);
     strcpy(s[recNumber].ps,TempS.ps);
     s[recNumber].power=TempS.power;

      recNumber++;
     if(recNumber>=total) /*如果S缓存的被占满重新分配空间*/
     {
      total+=USERINCREASE;
      t=realloc(s,total*sizeof(user));
      if(t==NULL)
      {
        printf("Allot memory fail!");
        printf("press any key to continu...");
        free(s);/*释放内存*/
        getch();
        return;
      }
      s=t;
      t=NULL;
     }
   }
  fclose(fp);
  /* 将删除后的结构休记录写入文件 */
  fp=fopen(DataFile,"wb+");
  if(fp==NULL)
  {
   printf("\nOpen file %s fail ! End with any key.\n",DataFile);
   perror("Open up fail");
   free(s);/*释放内存*/
   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!");
    free(s);/*释放内存*/
    getch();
    exit(1);
   }
  }
  fclose(fp);
  /* 显示删除后的文件 */
  fp=fopen(DataFile,"rb");
  if(fp==NULL)
  {
   printf("\nOpen file %s fail! End with any key\n",DataFile);
   perror("Open file fail");
   free(s);/*释放内存*/
   getch();
   exit(1);
  }
  printf("the file after delete is:\n");
  printf("\nNumber\t\tName\t\tpassword\t\tpower\n");
  while(fread(&TempS,sizeof(user),1,fp)!=(int)NULL)
  {
   if(TempS.Number!=0)
   printf("\n%ld\t\t%s\t\t%-8s\t\t%d",TempS.Number,TempS.Name,TempS.ps,TempS.power);
  }
  fclose(fp);
  printf("\nGo on?(y/n)");
  next=getch();
  putchar('\n');
  if(next=='y'||next=='Y')goto begin;
  free(s);/*释放内存*/
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -