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

📄 modnum.c

📁 c语言的一个课程设计。它包括函数的基本调用
💻 C
字号:
#ifndef MODIFYINTRO
 #define MODIFYINTRO
/*
修改候选人简介
*/
 #include "e:\vot\shead.h"
 #include "stdio.h"
 void ModifyIntro()
 {
 int i,j,k;
 long modnum;
 long Number;
 char Name[20];
 char intro[500];
 candidate TempS;
 candidate *s,*t;
 int recNumber;
 int total=CANDIDATENUM;   /*CANDIDATENUM ,在shead.h头文件中定义的常量,值为10*/
 char DataFile[40]="e:\\vot\\houshuan";char next;
 FILE * fp;/*fp指针指向存储数据的文件名*/
 s=(candidate *)malloc(CANDIDATENUM*sizeof(candidate));
 if(s==NULL)
 {
  printf("allot memory fail!\n");
  printf("press any key to continu...\n");
  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("intro(499characters):");
  scanf("%s", &intro);
  intro[499]='\0';

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

⌨️ 快捷键说明

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