大家好!以下程序运行时,不能正确读取f:\\18.txt中的数据,显示出来有很多乱码,能详细解释一下吗?谢谢!
#include<stdio.h>
#include<stdlib.h>
struct student
{long int num;
char name[20];
char major[25];};
FILE *fp;
main()
{int i,num,realnum;
struct student st,*pst;
printf(“input student number:”);
scanf(“%d”,&num);
pst=(struct student *)malloc(num*sizeof(st));
if(!pst)
return;
fp=fopen(“f:\\18.txt”,”rt”);
if(NULL==fp)
{free(pst);return;}
realnum=0;
printf(“%s\t%s\%s\%s\n”,”number”,”name”,”major”);
for(i=0;i<num&&(!feof(fp));i++)
{fread(pst+i,sizeof(struct student),1,fp);
realnum++;
st=*(pst+i);
printf(“realnum=%d\n”,realnum);
printf(“%ld\t%s\t%s\n”,st.num,st.name,st.major);
}
free(pst);fclose(fp);
}
#include<stdlib.h>
struct student
{long int num;
char name[20];
char major[25];};
FILE *fp;
main()
{int i,num,realnum;
struct student st,*pst;
printf(“input student number:”);
scanf(“%d”,&num);
pst=(struct student *)malloc(num*sizeof(st));
if(!pst)
return;
fp=fopen(“f:\\18.txt”,”rt”);
if(NULL==fp)
{free(pst);return;}
realnum=0;
printf(“%s\t%s\%s\%s\n”,”number”,”name”,”major”);
for(i=0;i<num&&(!feof(fp));i++)
{fread(pst+i,sizeof(struct student),1,fp);
realnum++;
st=*(pst+i);
printf(“realnum=%d\n”,realnum);
printf(“%ld\t%s\t%s\n”,st.num,st.name,st.major);
}
free(pst);fclose(fp);
}
>> 本文固定链接: http://www.vcgood.com/archives/3481
%s显示的字符串是以’\0′结尾的。