#f include #f include truct student/*定义结构体类型 student及数且*/ char number [6]; char name[201; }s2}={"21531","李阳”m,19,270.5},("21532""宋美",',18,292}}; struct student ss2],/*定义结构体类型数组s用以存放从文件中读出的数据* FILE* tp if((fp=fopen("e: stu. txt", wb+))==NULL) printi("无法打开文件!n") exit(O); if(fwrite(&s[i] j, 1, tp)l=1) printi("写入文件时出错!n") rewind(fp) printf(读出文件内容:Ⅶn”) for(i=0;i<=1;i++) fread(&ss[i]j, 1, fp); printf("%s %s %c %d %d\n", ss(i]. number, ss[]. name, ss[]. sex, ss[i]. age, ss[].score) fclose(fp)
# include # include main() { struct student /*定义结构体类型 student 及数且 s[]*/ { char number[6]; char name[20]; char sex; int age; int score; } s[2]={{ "21531","李阳",'m',19,270.5},{"21532","宋美", 'f', 18,292} }; struct student ss[2]; /*定义结构体类型数组 ss[],用以存放从文件中读出的数据*/ int i,j; FILE * fp; if((fp=fopen("e:\\stu.txt","wb+") ) == NULL) { printf("无法打开文件!\n"); exit(0); } j=sizeof(struct student); for(i=0;i<=1;i++) if(fwrite(&s[i] ,j,1 ,fp)!= 1) printf("写入文件时出错!\n"); rewind(fp); printf(“读出文件内容:\n”); for(i=0;i<=1;i++ ) { fread(&ss[i] ,j, 1, fp); printf("%s %s %c %d %d\n",ss[i].number, ss[i].name, ss[i].sex, ss[i].age, ss[i].score); } fclose(fp); }