📄 adduser.c
字号:
/* 添加用户 */
#include "stdio.h"
#include "user.h"
void AddUser()
{
FILE *fp=NULL;
user Show;
user TempS;
char DataFile[10]="yonghu";/* 存储用户信息文件名 */
int count=1;
fp=fopen(DataFile,"ab+");/* a+,当文件存在时,追加,当件不存在时,创建 */
clrscr();
if(fp==NULL)
{
printf("\nOpen file %s fail! End with any key.\n",DataFile);
perror("Open file fail");
getch();
exit(1);
}
/* 如果成功打开文件,则提示输入用户相关信息 */
printf("input number,name,password and grade,number is 0 means input is end.\n");
printf("Number is not exceed 9 figures,Name is not exceed 20 characters ,range of grade:0~1");
/* 循环从键盘上读取用户输入的用户相关信息 */
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("password:");
scanf("%s",&TempS.ps);
TempS.ps[7]='\0';
printf("grade:");
scanf("%d",&TempS.power);
printf("\n");
/* 如遇无法写入文件的异常,则加以提示 */
if(fwrite(&TempS,sizeof(user),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 %s in file.\n",DataFile);
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);
}
printf("\nNumber\t\tName\t\tpassword\t\tpower\n");
while(fread(&Show,sizeof(user),1,fp)!=(int)NULL)
{
printf("\n%ld\t\t%s\t\t%-8s\t\t%d\n",Show.Number,Show.Name,Show.ps,Show.power);
}
fclose(fp);
printf("press any key to continu...");
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -