C语言结构体和文件问题
发布网友
发布时间:2023-01-07 21:04
我来回答
共1个回答
热心网友
时间:2023-10-20 20:03
#include<stdio.h>
#define N 3
struct Student { int number; float score; }
void main() { struct Student a[N],b[N]; int i; FILE *fp;
for ( i=0;i<N;i++ ) scanf("%d%f",&a[i].number,&a[i].score);
if ( fp=fopen("mark.dat","wb+") ) {
fwrite(a,sizeof(struct Student),N,fp);
fclose(fp);
if ( fp=fopen("mark.dat","rb") ) {
fread(b,sizeof(struct Student),N,fp);
fclose(fp);
for ( i=0;i<N;i++ )
if ( b[i].score>85 ) printf("%d %.1f\n",b[i].number,b[i].score);
} else printf("无法打开文件。\n");
} else printf("无法建立文件。\n");
}