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

📄 munum.c

📁 c语言的一个课程设计。它包括函数的基本调用
💻 C
字号:
/*
修改用户名,密码,权限信息
输入
1、要修改的用户所在文件名。
2、修改用户的具体信息
3、要
*/
#include "e:\vot\user.h"
#include "stdio.h"
void ModifyByUserNumber()
{
 int i,j,k;
 long modnum;
 long Number;
 char Name[20];
 char ps[8];
 int power;
 user TempS;
 user *s,*t;
 int total=SIZE;/*SIZE,在USER。H头文件中定义的常量,值为100*/
 int recNumber;
 char DataFile[40]="e:\\vot\\yonghu";char next;
 FILE * fp;/*fp指针指向存储数据的文件名*/
 s=(user*)malloc(total*sizeof(user));
 if(s==NULL)
 {
  printf("allot memory fail!\n");
  printf("press any key to continu...\n");
  free(s);/*释放内存*/
  getch();
  return;
 }
 begin:
  clrscr();
  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("please input the user's number which you will modify:");
  scanf("%ld",&modnum);
  printf("the user you will modify is:%ld\n",modnum);
  /* 输入要修改的记录的信息 */
  Number=modnum;

  printf("name:");
  scanf("%s",&Name);
  Name[19]='\0';

  printf("password:",&ps);
  scanf("%s",ps);
  ps[7]='\0';

  printf("grade:",&power);
  scanf("%d",&power);

  /* 将文件中要修改的信息存入结构体数组 */
  recNumber=0;
  while((fread(&TempS,sizeof(user),1,fp))!=(int)NULL)
  {
   if(TempS.Number!=modnum)
   {
      s[recNumber].Number=TempS.Number;
      strcpy(s[recNumber].Name,TempS.Name);
      strcpy(s[recNumber].ps,TempS.ps);
      s[recNumber].power=TempS.power;
   }
   else
   {
    s[recNumber].Number=Number;
    strcpy(s[recNumber].Name,Name);
    strcpy(s[recNumber].ps,ps);
    s[recNumber].power=power;
   }
    recNumber++;
   if(recNumber>=total)
   {
      total+=INCREAMENT;
      t=realloc(s,total*sizeof(user));
      if(t==NULL)
      {
       printf("allot memory to continu...");
       printf("");
       free(s); /*释放内存*/
       getch();
       return;
      }
      s=t;
      t=NULL;
   }
  }
  printf("\nrecNumber %d\n",recNumber);
  /* 将删除后的结构体记录写入文件 */
  fp=fopen(DataFile,"wb+");
  if(fp==NULL)
  {
   printf("\nSet up file %s fail!End with any key.\n",DataFile);
   perror("Set up fail");
   fflush(stdin);
   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 any key.\n",DataFile);
    perror("Write file faill");
    fflush(stdin);
    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");
    fflush(stdin);
    free(s); /*释放内存*/
    getch();
    exit(1);
  }
  printf("the file after modify 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\n",TempS.Number,TempS.Name,TempS.ps,TempS.power);
  }
  fclose(fp);
  /* 询问是否继续修改 */
  printf("Go on?(y/n)");
  fflush(stdin);
  next=getchar();
  putchar('\n');
  if(next=='y' || next=='Y')goto begin;
 free(s); /*释放内存*/
}

⌨️ 快捷键说明

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