📄 creat.cpp
字号:
//creat用于创建学生成绩信息文件
#include "creat.h"
void CreatFile()
{
FILE *fp = NULL;
Student 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("chinesescore:");
scanf("%f", &Tmps.chinesescore);
//提示输入学生数学成绩
printf("mathscoer:");
scanf("%f", &Tmps.mathscore);
//用公式自动计算学生总成绩
Tmps.heji = Tmps.chinesescore + Tmps.mathscore;
printf("\n");
//如果遇到无法写入文件的异常,则加以提示
if (fwrite(&Tmps, sizeof(Student), 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\tchinesescore\tmathscore\tjihe\n");
while (fread(&Tmps, sizeof(Student), 1, fp))
{
printf("\n%ld\t%s\t%4.2f\t%4.2f\t%4.2f\n", Tmps.Number, Tmps.Name, Tmps.chinesescore, Tmps.mathscore, Tmps.heji);
}
fclose(fp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -