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

📄 creat.c

📁 你想了解着个东西吗?请进来,着是很有用的!
💻 C
字号:
#ifndef CREATFILE
#define CREATFILE

#include <stdio.h>
#include "shead.h"
/*创建用户信息*/
void CreatFile()
{
 FILE *fp=NULL;
 candidate Show;
 candidate TempS;
 char DataFile[10]="houshuan";/* 存储投票人信息的文件名 */
 int count=0;
 fp=fopen(DataFile,"ab+");
 /* 如果当前文件不存在,提示打开文件失败 */
 if(fp==NULL)
 {
  printf("\nOpen file %s fail!End with any key.\n",DataFile);
  perror("Open file fail");
  getch();
  exit(1);
 }
 clrscr();
 /* 如果成功打开文件,则提示输入序号、用户名、密码、权限等相关信息 */
 printf("input number,name and intro.number is 0 means input is end.\n");
 printf("Number is not exceed 10 figures,Name is not exceed 20 characters");
 /* 循环从键盘上读取投票人输入的投票人相关信息 */
 while(1)
  {
   printf("\ninput 'number=0' means end input.\n");
   printf("number:");
   scanf("%ld",&TempS.Number);
   if(TempS.Number==0)
    {
     break;
    }
   printf("name:");
   scanf("%s",TempS.Name);
   TempS.Name[19]='\0';

   printf("intro(499characters):");
   scanf("%s",TempS.intro);
   TempS.intro[499]='\0';

   TempS.votenum=0;
   /* 如遇无法写入文件的异常,则加以提示 */
   if(fwrite(&TempS,sizeof(candidate),1,fp)!=1)
   {
    printf("\nwrite file %s fail! End with any key\n",DataFile);
    perror("Write file fail");
    getch();
    exit(1);
   }
   count++;
  }
  fclose(fp);
  /* 在屏幕上显示文件内容 */
  printf("The data you input is store successful !\n");
  printf("Content as follow:\n");
  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);
  }
  while(fread(&Show,sizeof(candidate),1,fp)!=(int)NULL)
  {
   printf("\nNumber:%ld\tName:%s\nintro:%s\nvote:%d\n",Show.Number,Show.Name,Show.intro,Show.votenum);
  /* printf("press any key to continu....");*/
  /*getch();*/
  }
  fclose(fp);
  printf("press any key to continu....");
  getch();
}
#endif

⌨️ 快捷键说明

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