#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
(008)从键盘输入N个学生的有关数据,然后把它们转出到磁盘文件中去并且从磁盘输出到屏幕显示。
最新推荐文章于 2022-12-06 15:42:18 发布