下面是我写的一段从读取文件内容再打印到屏幕的一段代码,但是打印的每行最后2个数总是不正确。用txt文件也是这样。
希望有高手来解释下。。。。。。。。。
void print_list()
{
student *head=read_list();
if(head==NULL) printf("文件无记录!");
while(head!=NULL){
printf("%d %s %.1f %.1f %.1f %.1f\n",head->num,head->name,head->math,head->eng,head->c_l,head->score);
head=head->next;
}
}
student *read_list()
{
student *head,*link,*p;
FILE *f;
if((f=fopen("student.xls","r"))==NULL){
printf("文件打开错误!\n");
exit(0);
}
head=link=NULL;
while(!feof(f)){
p=(student *)malloc(sizeof(student));
fscanf(f,"%d%s%f%f%f%f\n",&p->num,p->name,&p->math,&p->eng,&p->eng,&p->c_l,&p->score);
if(head==NULL){
head=p;
}
else link->next=p;
link=p;
p->next=NULL;
}
fclose(f);
return head;
}