cuser.cpp
来自「内有说明」· C++ 代码 · 共 110 行
CPP
110 行
//creatuser用于创建用户信息文件
#include "cuser.h"
void CreatUser()
{
FILE *fp = NULL;
User Tmps;
char DataFile[40] = "";
int count = 1;
//====输入存放用户信息的文件名======
printf("\nplease input new file of score.");
printf("\nnotice:name of file can't exceed 8 characters.suffix can't exceed 3 characters,part of exceed will be discarded.\n");
gets(DataFile);
//如果用户没有输入,则循环提示用户输入
while (*DataFile == ('\0'))
{
printf("\nplease input new file of score.");
printf("\nnotice:name of file can't exceed 8 characters.suffix can't exceed 3 characters,part of exceed will be discarded.\n");
gets(DataFile);
}
//用二进制写的方式打开文件,即创建文件
fp = fopen(DataFile, "wb+");
//如果当前文件不存在,提示打开文件失败
if (fp == NULL)
{
printf("\nopen file %s faile!end with any key.\n", DataFile);
perror("open file fail");
getch();
exit(1);
}
//如果成功打开或创建文件,则提示输入学生学号,姓名,密码,权限等相关信息
printf("input number,name and grade of english,math and cheinese.number is 0 means input is end.\n ");
printf("number is not exceed 9 figures,name is not exceed 20 characters,range od grade:0.00~1000.00\n");
//循环从键盘上读取用户输入的学号,姓名,密码,权限等相关信息
while (count == SIZE)
{
//输入学号,如为0则停止输入
printf("\ninput 'number=0' means end input.\n");
printf("number:");
scanf("%ld", &(Tmps.Number));
if (Tmps.Number == 0)
break;
//提示输入学生姓名
printf("name:");
scanf("%s", Tmps.Name);
//提示输入用户密码
printf("password:");
scanf("%s", Tmps.ps);
//提示输入用户权限
printf("power:");
scanf("%d", &Tmps.power);
//用公式自动计算学生总成绩
//Tpms.heji=Tpms.chineseescore+Tmps.mathscore;
printf("\n");
//如果遇到无法写入文件的异常,则加以提示
if (fwrite(&Tmps, sizeof(User), 1, fp) != 1)
{
printf("\nwrite file %s fail!end with any key\n", DataFile);
perror("write file fail!");
getch();
exit(1);
}
count++;
}
//如果输入的数据量超过最大允许的范围,则提示数据不能录入
if (count > SIZE)
printf("\nsorry,number of data can not exceed %d\n", SIZE);
fclose(fp);
//在屏幕上显示文件内容
//clrscr();
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\tname\tpassword\tpower\n");
while (fread(&Tmps, sizeof(User), 1, fp))
{
printf("\n%ld\t%s\t%s\t%d\n", Tmps.Number, Tmps.Name, Tmps.ps, Tmps.power);
}
fclose(fp);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?