struct stu { int num; char name[6]; char sex[3]; int dorm[3]; int tel[8]; } stua[N];
void main()
{ int i,j; FILE*fp;
if((fp=fopen(“A.txt”,"wb+"))==NULL) { printf("can not open the file.\n"); exit(0); }
for(i=0;i
{ printf("请输入数据:");
scanf("%d%s%s%d%d%d",
&stua[i].num,stua[i].name,stua[i].sex,
&stua[i].dorm[0],&stua[i].dorm[1],&stua[i].dorm[2]);
for ( j=0;j<8;j++ ) scanf("%d",,&stua[i].tel[j]);
if ( fwrite(&stua[i],sizeof(struct stu),1,fp)!=1) printf("file write error\n");
}
fclose(fp);
if((fp=fopen("A.txt","rb"))==NULL) {printf("can not open the file"); exit(0);}
for(i=0;i
{ if(fread(&stua[i],sizeof(cstruct stu),1,fp)!=1) printf("file read error\n");
printf("%d %s %s %d %d %d " ,stua[i].num ,stua[i].name ,stua[i].sex,
stua[i].dorm[0],stua[i].dorm[1],stua[i].dorm[2]);
for ( j=0;j<8;j++ ) printf("%d ",stua[i].tel[j] ); printf("\n");
}
fclose(fp);
}
取消
评论