fscanf()与fread()的区别
fscanf()与fread()之间有什么区别???有高手帮忙看一下我的这个程序
#define LONG 5
typedef struct td {char name[10];
char code[20];
int age;
}STUDENT;
void fun(STUDENT *std);
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
main()
{
STUDENT student[LONG];
FILE *fp;
char filename[20];
int count;
gets(filename);
fp=fopen(filename,"rb");
if(fp==NULL){
fprintf(stderr,"Error of opening");
exit(1);
}
for(count=0;count<LONG;count++)
fscanf(fp,"%s%s%d",student[count].name,student[count].code,student[count].age);
(这里我开始用的是fread()可是读不了文件中的内容)
fun(student);
for(count=0;count<LONG;count++)
printf("%s\n%s\n%d\n",student[count].name,student[count].code,student[count].age);
fclose(fp);
return 0;
}
void fun(STUDENT *std)
{
STUDENT p_student;
int i,j;
for(i=0;i<LONG;i++)
for(j=i+1;j<LONG;j++){
if(strcmp((std+i)->name,(std+j)->name)>0){
p_student=*(std+i);
*(std+i)=*(std+j);
*(std+j)=p_student;
}
else
if(strcmp((std+i)->name,(std+j)->name)==0)
if((std+i)->age<(std+j)->age){
p_student=*(std+i);
*(std+i)=*(std+j);
*(std+j)=p_student;
}
}
}
这个是文件中的内容
jiangjun 08170106 20
wangmin 08170122 21
jiangmin 08170107 22
guyang 08170104 20
yuzhuang 08170123 21
这个是输出的内容:
guyang
08170104
0
jiangjun
08170106
0
jiangmin
08170107
0
wangmin
08170122
0
yuzhuang
08170123
0
怎么输出会有错误呢??