从键盘输入5名学生的学号、姓名和年龄,存入结构体数组中,然后将他们转存到磁盘文件中,用fwrite和fread。
定义学生信息结构数组,直接用结构数组进行fwrite和fread
#include <stdio.h>
typedef struct _student
{
int id;
char name[20];
int age;
}student;
int main()
{
student stu[5];
//省略信息输入
FILE *fp1,*fp2;
fp1 = fopen("stu.txt","w");
if(fp1 != NULL)
fwrite(stu,sizeof(student),5,fp1);
fclose(fp1);
fp2 = fopen("stu.txt","r");
if(fp2 != NULL)
fread(stu,sizeof(student),5,fp2);
fclose(fp2);
return 0;
}