7.将第6题已排序的学生成绩文件进行插入处理。插入一个学生的3门课程成绩,程序先计算新插入学生的平均成绩,然后将它按成绩高低顺序插入,插入后建立一个新文件。
#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
struct Student_type
{
int num;
char name[10];
float score1;
float score2;
float score3;
float avg;
}stud[SIZE+1];
void save()
{
FILE *fp;
int i;
if((fp=fopen("students.dat","wb"))==NULL)
{
printf("can't open file!\n");
exit(0);
}
for(i=0;i<SIZE;i++)//在for语句中重新把排序好的数组stud的每个元素依次存储到fp指向的文件的中的每个结构体学生数据,每次存储1个数组stud元素
{
if(fwrite(&stud[i],sizeof(struct Student_type),1,fp)!=1)
{
printf("file write error\n");
}
}
fclose(fp);
}
int main()
{
FILE *fp;
int i,j;
struct Student_type temp,another;
printf("请输入插入的第6个学生的学号、姓名和三门课的成绩:");
scanf("%d%s%f%f%f",&another.num,another.name,&another.score1,&another.score2,&another.score3);
another.avg=(another.score1+another.score2+another.score3)/3;
printf("新插入的第6个学生的三门课的平均成绩为:%f\n",another.avg);
fp=fopen("stud.dat","ab");//ab是追加的意思,在这条语句里fp指向二进制文件stud的末尾
fwrite(&another,sizeof(struct Student_type),1,fp);//在二进制文件stud的末尾,把结构体变量another的值赋值给fp所指向的文件末尾
fclose(fp);
fp=fopen("stud.dat","rb");
for(i=0;i<SIZE+1;i++)//在for语句中把fp指向的文件的中的每个结构体学生数据依次存储到数组stud中,每次读入1个sizeof(struct Student_type)到数组f中
//直到for循环终止
{
fread(&stud[i],sizeof(struct Student_type),1,fp);
}
fclose(fp);
for(i=0;i<SIZE;i++)//按照平均分从小到大的顺序对数组stud排序
{
for(j=i+1;j<SIZE+1;j++)
{
if(stud[i].avg>stud[j].avg)
{
temp=stud[i];
stud[i]=stud[j];
stud[j]=temp;
}
}
}
save();
printf("插入一个学生数据并排序后的的学生数据如下:\n");
fp=fopen("students.dat","rb");
for(i=0;i<SIZE+1;i++)//为了验证磁盘文件students中是否已存在此数据,用for语句从students文件中读入数据到stud数组,然后向屏幕上输出
{
fread(&stud[i],sizeof(struct Student_type),1,fp);
printf("第%d个学生的学号、姓名、三门课的成绩以及平均分为:",i+1);
printf("%-4d %-10s %-10.2f %-10.2f %-10.2f %-10.2f\n",stud[i].num,stud[i].name,stud[i].score1,stud[i].score2,stud[i].score3,stud[i].avg);
}
fclose(fp);//关闭文件students.dat,防止它被误用
return 0;
}
在VS2019下,需将源文件的fopen和scanf做些修改:
#include<stdio.h>
#include<stdlib.h>
#define SIZE 5
struct Student_type
{
int num;
char name[10];
float score1;
float score2;
float score3;
float avg;
}stud[SIZE + 1];
void save()
{
FILE* fp;
int i;
fopen_s(&fp, "students.dat", "wb");
if (fp == NULL)
{
printf("can't open file!\n");
exit(0);
}
for (i = 0; i < SIZE; i++)//在for语句中重新把排序好的数组stud的每个元素依次存储到fp指向的文件的中的每个结构体学生数据,每次存储1个数组stud元素
{
if (fwrite(&stud[i], sizeof(struct Student_type), 1, fp) != 1)
{
printf("file write error\n");
}
}
fclose(fp);
}
int main()
{
FILE* fp;
int i, j;
struct Student_type temp, another;
printf("请输入插入的第6个学生的学号、姓名和三门课的成绩:");
scanf_s("%d%s%f%f%f", &another.num, another.name, (unsigned int)sizeof(another.name), &another.score1, &another.score2, &another.score3);
another.avg = (another.score1 + another.score2 + another.score3) / 3;
printf("新插入的第6个学生的三门课的平均成绩为:%f\n", another.avg);
fopen_s(&fp, "stud.dat", "ab");//ab是追加的意思,在这条语句里fp指向二进制文件stud的末尾
fwrite(&another, sizeof(struct Student_type), 1, fp);//在二进制文件stud的末尾,把结构体变量another的值赋值给fp所指向的文件末尾
fclose(fp);
fopen_s(&fp, "stud.dat", "rb");
for (i = 0; i < SIZE + 1; i++)//在for语句中把fp指向的文件的中的每个结构体学生数据依次存储到数组stud中,每次读入1个sizeof(struct Student_type)到数组f中
//直到for循环终止
{
fread(&stud[i], sizeof(struct Student_type), 1, fp);
}
fclose(fp);
for (i = 0; i < SIZE; i++)//按照平均分从小到大的顺序对数组stud排序
{
for (j = i + 1; j < SIZE + 1; j++)
{
if (stud[i].avg > stud[j].avg)
{
temp = stud[i];
stud[i] = stud[j];
stud[j] = temp;
}
}
}
save();
printf("插入一个学生数据并排序后的的学生数据如下:\n");
fopen_s(&fp, "students.dat", "rb");
for (i = 0; i < SIZE + 1; i++)//为了验证磁盘文件students中是否已存在此数据,用for语句从students文件中读入数据到stud数组,然后向屏幕上输出
{
fread(&stud[i], sizeof(struct Student_type), 1, fp);
printf("第%d个学生的学号、姓名、三门课的成绩以及平均分为:", i + 1);
printf("%-4d %-10s %-10.2f %-10.2f %-10.2f %-10.2f\n", stud[i].num, stud[i].name, stud[i].score1, stud[i].score2, stud[i].score3, stud[i].avg);
}
fclose(fp);//关闭文件students.dat,防止它被误用
return 0;
}