#include "stdio.h"
#define SIZE 1
struct teacher
{
int num;
char name[8];
char tel[16];
char addr[20];
}teacher[SIZE];
void save()
{
FILE *fp;
int i;
if((fp=fopen("teacher.txt","wb"))==NULL)
{
printf("文件打不开");
return;
}
for(i=0;i<SIZE;i++)
if(fwrite(&teacher[i],sizeof(struct teacher),1,fp)!=1)
printf("写入错误");
// fprintf(fp,"%d %s %s %s",teacher[i].num,teacher[i].name,teacher[i].tel,teacher[i].addr);
}
void display()
{
int i=0;
FILE *fp;
//struct teacher teacher[SIZE];
if((fp=fopen("teacher.txt","rb"))==NULL)
{
printf("文件打不开");
return;
}
while(!feof(fp))
{
fread(&teacher[i],sizeof(struct teacher),1,fp);
printf("%6d %8s %16s %20s",&teacher[i].num,&teacher[i].name,&teacher[i].tel,&teacher[i].addr);
printf("\n");
i++;
}
}
main()
{
int i;
printf("输入教师信息 num name-8 tel-16 addr-20 \n ");
for(i=0;i<SIZE;i++)
scanf("%d%s%s%s",&teacher[i].num,&teacher[i].name,&teacher[i].tel,&teacher[i].addr);
save();
display();
}