c语言,把文件中数据输入到链表中,为何出错? 20
structper*readfile(structper*head,int*n,charaddress[30]){structper*p=NULL,*pr=head;FI...
struct per *readfile(struct per *head,int *n,char address[30])
{
struct per *p= NULL,*pr = head;
FILE *fp = NULL;
if((fp = fopen(address,"r")) == NULL)
{
printf("打开文件失败\n");
exit(0);
}
while(feof(fp) == 0)
{
p = (struct per *)malloc(sizeof(struct per));
if(p == NULL)
{
printf("no");
}
if(head == NULL)
{
head = p;
}
else
{
while(pr->next != NULL)
{
pr = pr->next;
}
pr->next = p;
}
fscanf(fp,"%11lf",&p->number);
fscanf(fp,"%10s",p->name);
fscanf(fp,"%2d",&p->age);
fscanf(fp,"%1d",&p->sex);
fscanf(fp,"%1d",&p->org);
p->next = NULL;
(*n)++;
}
fclose(fp);
return head;
} 展开
{
struct per *p= NULL,*pr = head;
FILE *fp = NULL;
if((fp = fopen(address,"r")) == NULL)
{
printf("打开文件失败\n");
exit(0);
}
while(feof(fp) == 0)
{
p = (struct per *)malloc(sizeof(struct per));
if(p == NULL)
{
printf("no");
}
if(head == NULL)
{
head = p;
}
else
{
while(pr->next != NULL)
{
pr = pr->next;
}
pr->next = p;
}
fscanf(fp,"%11lf",&p->number);
fscanf(fp,"%10s",p->name);
fscanf(fp,"%2d",&p->age);
fscanf(fp,"%1d",&p->sex);
fscanf(fp,"%1d",&p->org);
p->next = NULL;
(*n)++;
}
fclose(fp);
return head;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询