用了动态链表输出文件内容,但是运行到最后总是出现debug assertion failed failed fscanf.c line 54
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define len sizeof(struct student)
struct student
{
int number;
char name[20];
struct student *next;
}
main()
{
struct student *creat(void);
void print(struct student *p4);
struct student *p4,*p3;
p3=creat();
print(p3);
}
struct student *creat(void)
{
struct student *p1,*p2,*p;
int i=1;
FILE *fp;
if((fp=fopen("name2.txt","r")==NULL))
{
printf("cannot open");
exit(0);
}
p1=(struct student *)malloc(len);
if((p1=(struct student *)malloc(len)) == NULL)
{
printf("不能成功分配存储空间。");
exit(1);
}
else printf("hello");
fscanf(fp,"%d%s\n",&p1->number,&p1->name);
p2=p1;
while(p1->number!=2012210420)
{
if(i>1)
{
p2->next=p1;
p2=p1;
}
else p=p1;
i++;
p1=(struct student *)malloc(len);
if((p1=(struct student *)malloc(len)) == NULL)
{
printf("不能成功分配存储空间。");
exit(1);
}
fscanf(fp,"%d%s\n",&p1->number,&p1->name);//???????????????
}
p2->next=NULL;
free(fp);
fclose(fp);
return(p);
}
void print(struct student *p4)
{
struct student *p;
p=p4;
do
{
printf("%d %s\n",p->number,p->name);
p=p->next;
}while(p!=NULL);
}
相关文件.rar
(1.68 KB)