关于c++类链表读取TXT文件的问题

读取TXT文件不能读取最后一行,而且程序还会崩溃,这是怎么回事啊?代码如下:#include<iostream>#include<fstream>#include<str... 读取TXT文件不能读取最后一行,而且程序还会崩溃,这是怎么回事啊?
代码如下:
#include<iostream>
#include<fstream>
#include<string>
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);
}
展开
 我来答
cuenk
2017-04-10 · TA获得超过2.2万个赞
知道大有可为答主
回答量:2335
采纳率:71%
帮助的人:984万
展开全部
createlist函数改成这样
void creatList(student *&sPeople)
{
ifstream readFile("F:\\student.txt", ios::in);
student *s, *p = NULL;
while (!readFile.eof())
{
s = new student;
readFile >> s->num >> s->name >> s->score;
if (sPeople == NULL)
sPeople = s;
else
p->next = s;
p = s;
}
p->next = NULL;
return;
}
最后一行读不到是因为读取最后一行时
s = new student;
readFile >> s->num >> s->name >> s->score;
然后循环结束,你一个delete s,把最后一行的数据删除了
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式