📄 cex_13_3.c
字号:
#include "stdio.h"
#define SIZE 4
struct student_type
{
char name[10];
int num;
int age;
char addr[15];
}stud[SIZE];
void save(); /* 原型 */
void load(); /* 原型 */
void main()
{ int i;
for(i=0; i<SIZE; i++) /* 从键盘读入数据 */
scanf("%s%d%d%s",stud[i].name,&stud[i].num,&stud[i].age,stud[i].addr);save(); /* 存盘 */
save();
load();
for(i=0; i<SIZE; i++) /* 屏幕上显示 */
printf("%-10s%4d%4d%-15s\n", stud[i].name, stud[i].num, stud[i].age, stud[i].addr);
}
void save() /* 写数据到磁盘文件*/
{ FILE *fp;
int i;
if ((fp=fopen("stu_list", "wb"))==NULL)
{
printf("can not open file\n");
exit(0);
}
for(i=0; i<SIZE; i++)
if (fwrite(&stud[i], sizeof(struct student_type), 1, fp) != 1)
printf("file read error\n");
fclose(fp);
}
void load() /* 从盘读出 */
{ FILE *fp;
int i;
if ((fp=fopen("stu_list", "rb"))==NULL)
{
printf("can not open file\n");
return;
}
for(i=0; i<SIZE; i++)
if (fread(&stud[i], sizeof(struct student_type), 1, fp) != 1)
{
if (feof(fp)) return;
printf("file read error\n");
}
fclose(fp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -