fwrite fread fprintf fscanf 的问题
程序代码:
#include<stdio.h> #include<stdlib.h> struct student { int num; char name[10]; float score[3]; float aver; }stu[5]; int main(void) { FILE *fp; char filename[20]; int i,j; float sum; for(i=0;i<5;i++) { sum=0; printf("\ninput score of student %d\n",i+1); printf("NO: "); scanf("%d",&stu[i].num); printf("name:"); scanf("%s",stu[i].name); for(j=0;j<3;j++) { printf("score %d:",j+1); scanf("%f",&stu[i].score[j]); sum=sum+stu[i].score[j]; } stu[i].aver=sum/3; } puts("please input the filename:"); scanf("%s",filename); if((fp=fopen(filename,"w"))==NULL) { printf("file can not open\n!"); exit(0); } for(i=0;i<5;i++) { if(fwrite(&stu[i],sizeof(struct student),1,fp)!=1)//fprintf(fp,"%d %s %f %f %f %f",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].aver); { printf("file write error!\n"); } } fclose(fp); fp=fopen(filename,"r"); for(i=0;i<5;i++) { fread(&stu[i],sizeof(struct student),1,fp);//fcanf(fp,"%d %s %f %f %f %f",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].aver); printf("\n%d ,%s, %.2f, %.2f, %.2f, %.2f\n",stu[i].num,stu[i].name,stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].aver); } fclose(fp); return 0; }用fwite和fread的时候,能够正确的在屏幕上显示出来,在文件中是乱码,我想让在文件中不是乱码,就用了fprintf fscanf 但是老不成功啊,不知道咋回事,我的代码如上,希望高手给予指教