新手求助,文件写入和读取的时候出问题。

很菜的菜鸡 2018-08-22 10:08:33

这个是第一次运行时写入文件的信息,两个00000000都是Time*的空指针

这个是文件第二次运行的结果。
调试的时候发现应该是下面这步出了问题

if语句是因为如果没有就会有下面的问题

以下是关于读和写以及Time类运算符重载的相关代码

int Worker::read(Worker * head)
{
ofstream infile;
infile.open("签到.txt", ios::app);
if (!infile)
{
return 0;
}
else
{
infile.close();
ifstream infile;
infile.open("签到.txt", ios::in);
while (!infile.eof())
{
Worker* newnode = new Worker;
infile >> newnode->Number;
if (newnode->Number.length() == 0)
{
delete newnode;
break;
}
infile >> newnode->Name;
infile >> newnode->sex;
infile >> newnode->age;
infile >> newnode->Number;
infile >> newnode->Phone;
if (newnode->start != NULL && newnode->end != NULL)
{
infile >> newnode->start;
infile >> newnode->end;
}


head->next = newnode;
head = head->next;
}
infile.close();
}
return 0;
}

void Worker::write(Worker * t)

{
ofstream outfile;
outfile.open("签到.txt", ios::out);
while (t->next != NULL)
{
outfile << t->next->Number << '\t';
outfile << t->next->Name << '\t';
outfile << t->next->sex << '\t';
outfile << t->next->age << '\t';
outfile << t->next->Phone << '\t';
outfile << t->start << '\t';
outfile << t->end << endl;
t = t->next;
}
outfile.close();
}



istream & operator>>(istream & input, Time *& t)
{
input >> t->year >> t->month >> t->day >> t->hour >> t->minute >> t->second;
return input;
// TODO: 在此处插入 return 语句
}
...全文
407 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
shwill123 2018-08-29
  • 打赏
  • 举报
回复
1# 楼正确,要先判断 newnode是否为空
yiyefangzhou24 2018-08-23
  • 打赏
  • 举报
回复
如果newnode如果为空,哪来的start和end指针??这样读取肯定会出错,应该判断newnode是否为null
只此冒泡君 2018-08-23
  • 打赏
  • 举报
回复
不明白 你这start 和end 是什么鬼,其次你写入用\t 读取的时候不做处理

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧