c++ 链表输出文件中的数据
#include<fstream.h>
struct student
{
long int num;
char name[8];
char sex[2];
int age;
char major[8];
student *next;
};
student *creatlist()
{
student *head=NULL,*tail=NULL;
student *p=NULL;
fstream file1;
file1.open("data.txt",ios::in|ios::binary);
while(!file1.eof())
{
p=new student;
file1>>p->num>>p->name>>p->sex>>p->age>>p->major;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
}
delete p;
tail->next=NULL;
file1.close();
return head;
}
void showlist(student *head)
{
student *tail;
tail=head;
while(tail)
{
cout<<tail->num<<" "<<tail->name<<" "<<tail->sex<<" "<<tail->age<<" "<<tail->major<<endl;
tail=tail->next;
}
}
void main()
{
student *head=NULL;
head=creatlist();
showlist(head);
}
data中为:
01 张三 男 19 通信1082
02 李四 男 20 通信1082
03 王二 男 20 通信1082
我的程序能输出前面两条信息,最后一条信息输出的是 -572662307 葺葺葺葺葺葺葺葺葺葺葺葺 葺葺葺葺葺葺葺葺 -572662307 葺葺葺葺
请高手指点啊,我的程序哪错了。
我用的是vc6.0的