修改以下代码“”“#include<stdio.h> #define N 5 typedef struct student{ long sno; char name[10]; float score[3]; }STU; void fun(char *filename,STU n){ FILE *fp; /**********found**********/ fp=fopen(filename,"rb"); /*********”
时间: 2023-07-06 13:38:14 浏览: 127
#include <stdio.h>
#define N 5
typedef struct student {
long sno;
char name[10];
float score[3];
} STU;
void fun(char *filename, STU n) {
FILE *fp;
fp = fopen(filename, "rb");
if (fp == NULL) {
printf("Cannot open file!\n");
return;
}
fread(&n, sizeof(STU), 1, fp);
printf("sno:%ld\nname:%s\nscore[0]:%.2f\nscore[1]:%.2f\nscore[2]:%.2f\n", n.sno, n.name, n.score[0], n.score[1], n.score[2]);
fclose(fp);
}
int main() {
STU s[N] = {
{20210001, "Tom", {89, 78, 92}},
{20210002, "Jerry", {78, 87, 91}},
{20210003, "Mickey", {90, 77, 83}},
{20210004, "Minnie", {94, 92, 90}},
{20210005, "Donald", {85, 88, 82}}
};
FILE *fp;
fp = fopen("test.dat", "wb");
if (fp == NULL) {
printf("Cannot open file!\n");
return 0;
}
fwrite(&s[2], sizeof(STU), 1, fp);
fclose(fp);
fun("test.dat", s[2]);
return 0;
}
```
阅读全文