关于c++类链表读取文本文件的问题 5C

读取时总是读取不了TXT文件的最后一行内容,而且运行到最后程序还会崩溃,这是怎么回事呢?
TXT文件内容如下:
1 老王 50
2 小李 65
3 大刘 98
4 天天 10
代码如下:
#include
#include
#include
using namespace std;
class student
{
public:
int num;
string name;
int score;
student *next;
void creatList(student *& sPeople)
{
ifstream readFile("F:\student.txt", ios::in);
student *s, *p = NULL;
s = new student;
readFile >> s->num >> s->name >> s->score;
while (!readFile.eof())
{
if (sPeople == NULL)
sPeople = s;
else
p->next = s;
p = s;
s = new student;
readFile >> s->num >> s->name >> s->score;
}
p->next = NULL;
delete s;
return;
}
void showList(student *sPeople)
{
while (sPeople)
{
cout << "学号:" << sPeople->num << "\t姓名:" << sPeople->name << "\t分数:" << sPeople->score << endl;
sPeople = sPeople->next;
}
}
};
int main()
{
student *sPeople = NULL;
sPeople->creatList(sPeople);
sPeople->showList(sPeople);
}

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问