为什么输入数据后再读取链表,链表最后一个节点的数据总是会重复输出两次代码:#include#include#define LEN sizeof(struct student)struct student{char name[20];long num;float chinese;float math;float english;struct student *next;};struct student *head;
main(){struct student *p,*q;p=(struct student*)malloc(LEN); head=p;head->next=NULL;q=(struct student*)malloc(LEN);FILE *f1;if((f1=fopen("无.dat","wb"))==NULL){printf("文件打开失败\n");exit(1);}printf("\n\t1:新建学生成绩表\n");printf("请在下面依次输入姓名学号语文数学英语\n");scanf("%s%ld%f%f%f",&q->name,&q->num,&q->chinese,&q->math,&q->english);while(q->name!=0&&q->num!=0&&q->chinese!=0&&q->math!=0&&q->english!=0){p->next=q;p=q;q=(struct student*)malloc(LEN);printf("请在下面依次输入学号姓名语文数学英语\n");scanf("%s%ld%f%f%f",&q->name,&q->num,&q->chinese,&q->math,&q->english);fwrite(p,sizeof(struct student),1,f1)!=1;}p->next=NULL;
if(fclose(f1)){printf("文件异常关闭");exit(1);}
if((f1=fopen("无.dat","rb"))==NULL){printf("文件打开失败\n");exit(1);}
p=head->next;while(!feof(f1)){fread(p,sizeof(struct student),1,f1);printf("%s %ld %f %f %f\n",p->name,p->num,p->chinese,p->math,p->english);}if(fclose(f1)){printf("文件异常关闭");exit(1);}}