我下面这段代码是哪里出错了? 写入文件没有问题 把文件里的东西写入链表 之后在输出链表 只输出第一行数据 之后都是0
#include "stdio.h"
#include "stdlib.h"
int n=0;
struct stu
{
int num;
char name[100];
float score;
struct stu *next;
};
void str(struct stu *h)
{
while(h!=NULL)
{
printf("%d %s %f\n",h->num,h->name,h->score);
h=h->next;
}
}
void w(struct stu *h)
{
FILE *file;
char fname[100];
int i;
struct stu *pthis,*head,*p1,*p2;
printf("请输入要写入的文件名\n");
scanf("%s",fname);
file=fopen(fname,"w");
if(file==NULL)
{
printf("打开文件错误\n");return;
}
printf("\n\n链表内容将被写入文件\n");
if(h==NULL)
{
printf("错误,没有数据!\n");
return;
}
pthis=h;
while(pthis!=NULL)
{
fprintf(file,"%d %s %f",pthis->num,pthis->name,pthis->score);
//fwrite(pthis,sizeof(struct stu),1,file);
pthis=pthis->next;
}
fclose(file);
file=fopen(fname,"r");
p1=p2=(struct stu *)malloc(sizeof(struct stu));
fscanf(file,"%d %s %f",&p1->num,p1->name,&p1->score);
head=p1;
//fread(p1,sizeof(struct stu),1,file);
//printf("%d %s %f",p1->num,p1->name,p1->score);
while(n>0)
{
p1=(struct stu *)malloc(sizeof(struct stu));
p2->next=p1;
fscanf(file,"%d %s %f",&p1->num,p1->name,&p1->score);
//p2->next=p1;
p2=p1;
n--;
}
str(head);
fclose(file);
}
main()
{
struct stu *head,*p1,*p2;
head=NULL;
p1=p2=(struct stu *)malloc(sizeof(struct stu));
printf("依次输入学号、姓名、成绩\n");
scanf("%d%s%f",&p1->num,p1->name,&p1->score);
head=p1;
while(p1->num!=0)
{
n++;
p1=(struct stu *)malloc(sizeof(struct stu));
printf("依次输入学号、姓名、成绩\n");
scanf("%d%s%f",&p1->num,p1->name,&p1->score);
p2->next=p1;
p2=p1;
}
str(head);
fflush(stdin);
w(head);
}