8.将第7题结果仍存入原有的stu_sort文件而不另建立新文件。

8.将第7题结果仍存入原有的stu_sort文件而不另建立新文件。

#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("stu_sort.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("stu_sort.dat","rb");
	for(i=0;i<SIZE+1;i++)//为了验证磁盘文件stu_sort中是否已存在此数据,用for语句从stu_sort文件中读入数据到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);
	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, "stu_sort.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, "stu_sort.dat", "rb");
	for (i = 0; i < SIZE + 1; i++)//为了验证磁盘文件stu_sort中是否已存在此数据,用for语句从stu_sort文件中读入数据到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);
	return 0;
}

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值