(008)从键盘输入N个学生的有关数据,然后把它们转出到磁盘文件中去并且从磁盘输出到屏幕显示。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 3
#define M 3
/*
auther:新生代小码农
date:2021-09-18
Question:从键盘输入N个学生的有关数据,然后把它们转出到磁盘文件中去,
         并且从磁盘输出到屏幕显示。 
          

*/
struct Student{
	int num;
	char name[50];
	float score[N];
	float aver;
}stu[M];

void readIn(struct Student stu[]){
	FILE *fp;
	int i;
	if((fp=fopen("student.dat","wb"))==NULL){
		printf("cannot open file\n");
		return; 
	}
		for(i=0;i<M;i++){//将内存中的学生数据输出到磁盘中去 
			if(fwrite(&stu[i],sizeof(struct Student),1,fp)!=1){//等于1表示在写入 
				printf("file write error\n");	
			}
		}
			fclose(fp);
			printf("file write have successed\n"); 
}
void writeOut(struct Student stu[]){
	FILE *fp;
	int i,j;
	if((fp=fopen("student.dat","rb"))==NULL){
	 	printf("cannot open file\n");
		exit(0);
		 }else{ 
		for(i=0;i<M;i++){//从fp指向的文件中读入结构体数组stu的元素 
			fread(&stu[i],sizeof(struct Student),1,fp);//等于1表示在写入 
			printf("num:%3d\tname:%s\tscore:%.2f\t%.2f\t%.2f\taver:%.3f\t",stu
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值