从键盘输入5名学生的学号、姓名和年龄,存入结构体数组中,然后将他们转存到磁盘文件中,用fwrite和fread。
1条回答 默认 最新
- CSDN专家-link 2021-12-20 11:28关注
定义学生信息结构数组,直接用结构数组进行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; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决评论 打赏 举报 编辑记录无用 2