求C语言高手找错
这是一个从文件中读入链表的函数,参数是一个文件指针 如果文件为空就返回NULL,否则返回链表头指针。可是连不到链表,求高手指错!!!
结构体为
struct wupin
{
char sort[20];//种类
char name[50];//物品名
int code;//编号
int year1;//生产年
int month1;//生产月
int day1;//生产日
int year2;//有效年
int month2;//有效月
int day2;//有效日
struct wupin*next;
};
struct wupin*read2(FILE* fp)
{
int ch;
int i=0;
struct wupin*head,*p1,*p2;
ch=fgetc(fp);
if(ch==EOF)
return NULL;
fseek(fp,0L,0);
head=p1=p2=(struct wupin*)malloc(sizeof(struct wupin));
fscanf(fp,"%s",p1->sort);
fscanf(fp,"%s",p1->name);
fscanf(fp,"%d",&p1->code);
fscanf(fp,"%d %d %d",&p1->year1,&p1->month1,&p1->day1);
fscanf(fp,"%d %d %d",&p1->year2,&p1->month2,&p1->day2);
p1->next=NULL;
while(!feof)
{
p2=p1;
p1=(struct wupin*)malloc(sizeof(struct wupin));
p2->next=p1;
fscanf(fp,"%s",p1->sort);
fscanf(fp,"%s",p1->name);
fscanf(fp,"%d",&p1->code);
fscanf(fp,"%d %d %d",&p1->year1,&p1->month1,&p1->day1);
fscanf(fp,"%d %d %d",&p1->year2,&p1->month2,&p1->day2);
p1->next=NULL;
}
free(p2);
while(head!=NULL)
{
printf("%s",head->sort);
head=head->next;
}
return head;
}
[解决办法]
while(!feof) //这里feof没有参数,应该是!feof(fp)
{
p2=p1;
p1=(struct wupin*)malloc(sizeof(struct wupin));
p2->next=p1;
fscanf(fp,"%s",p1->sort);
fscanf(fp,"%s",p1->name);
fscanf(fp,"%d",&p1->code);
fscanf(fp,"%d %d %d",&p1->year1,&p1->month1,&p1->day1);
fscanf(fp,"%d %d %d",&p1->year2,&p1->month2,&p1->day2);
p1->next=NULL;
}
[解决办法]
还有feof加上参数才可以。