从文件中读取内容问题

smdszgzh 2009-03-17 11:13:30

Node* StudentList::ReadFile(char *filename)
{
ifstream m_Ifile;
m_Ifile.open(filename,ios::in);
string line,word;
if(!m_Ifile)
{
cout<<"打开文件失败!"<<endl;
return NULL;
}
m_Ifile.seekg(0,ios_base::end); //将文件指针移动到文件尾部
if(!m_Ifile.tellg()) //文件长度为0,返回
{
cout<<"不存在任何学生信息,请添加!"<<endl;
return NULL;
}
m_Ifile.seekg(0); //将文件指定重新移动到首位置
while(!m_Ifile.eof()) //读取学生信息
{
pNew=new Node();
m_Ifile>>pNew->sID;
m_Ifile>>pNew->sName;
m_Ifile>>pNew->bSex;
m_Ifile>>pNew->Score[0];
m_Ifile>>pNew->Score[1];
if(pHead==NULL)
{
pTail=pHead=pNew;
}
else
{
pTail->pNext=pNew;
pTail=pTail->pNext;
}
pTail->pNext=NULL;

}
cout<<"加载学生信息成功!"<<endl;
return pHead;







void StudentList::ShowStudent(StudentList &list)
//setiosflags(ios::left)控制输出格式左对齐,要包含头文件<iomanip>
//setw()控制输出宽度,不足用空格添充
{
Node* p=pHead;
if(p==NULL)
{
cout<<"不存在任何学生的信息!请先添加!"<<endl;
cout<<"——————————————————————————"<<endl;
return;
}
cout<<setiosflags(ios::left)<<setw(15)<<"ID"<<setw(15)<<"Name"<<setw(15)
<<"Sex"<<setw(15)<<"English"<<setw(15)<<"Math"<<endl;
while(p!=NULL)
{
cout<<setiosflags(ios::left)<<setw(15)<<p->sID<<setw(15)<<p->sName<<setw(15)
<<p->bSex<<setw(15)<<p->Score[0]<<setw(15)<<p->Score[1]<<endl;
p=p->pNext;

当从文件里读取再输出时会这样,不从文件里读就没问题,应该是文件读的结束标志没有弄好
高手帮看看
001 lzz boy 78 54
oo2 lisi boy 98 98
-6.27744e+066 -6.27744e+066
...全文
221 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
smdszgzh 2009-03-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 tangshuiling 的回复:]
引用 5 楼 smdszgzh 的回复:
引用 1 楼 tangshuiling 的回复:
C/C++ code
把while(!m_Ifile.eof()) 换成while(getline(line,m_Ifile))的形式试试
Node* StudentList::ReadFile(char *filename)
{
ifstream m_Ifile;
m_Ifile.open(filename,ios::in);
string line,word;
stringstream os;
if(!m_Ifile)
{
cout < <"打开文件失败!" < <endl;
return NULL;

[/Quote]
有len!=m_Ifile.tellg()做循环条件不行!
chin_chen 2009-03-17
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 tangshuiling 的回复:]
很多朋友认为文件尾有EOF,这是错误的。EOF是流的状态标志。在 C++中,是在读取文件失败时才产生EOF,因此,
楼主的代码用m_Ifile.eof()作为循环条件并不合适,会造成先读后置位的现象!
就你的代码还可改成:while(len!=m_Ifile.tellg()) len表示文件的总长度!
[/Quote]
不是这个原因吧 。我在我机器上试了一下。是可以结束的。我的文本文件里面存放的是一些字符,做测试,然后,
while(!m_Ifile.eof())
{
m_Ifile>>ch;

}
文件可以结束的。
而且我还看了这个函数的说明:
This flag is set by all standard input operations when the End Of File is reached in the sequence associated with the stream.


tangshuiling 2009-03-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 smdszgzh 的回复:]
引用 1 楼 tangshuiling 的回复:
C/C++ code
把while(!m_Ifile.eof()) 换成while(getline(line,m_Ifile))的形式试试
Node* StudentList::ReadFile(char *filename)
{
ifstream m_Ifile;
m_Ifile.open(filename,ios::in);
string line,word;
stringstream os;
if(!m_Ifile)
{
cout < <"打开文件失败!" < <endl;
return NULL;
}
m_Ifile.seekg(0,ios_…
[/Quote]
很多朋友认为文件尾有EOF,这是错误的。EOF是流的状态标志。在 C++中,是在读取文件失败时才产生EOF,因此,
楼主的代码用m_Ifile.eof()作为循环条件并不合适,会造成先读后置位的现象!
就你的代码还可改成:while(len!=m_Ifile.tellg()) len表示文件的总长度!
smdszgzh 2009-03-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tangshuiling 的回复:]
C/C++ code
把while(!m_Ifile.eof()) 换成while(getline(line,m_Ifile))的形式试试
Node* StudentList::ReadFile(char *filename)
{
ifstream m_Ifile;
m_Ifile.open(filename,ios::in);
string line,word;
stringstream os;
if(!m_Ifile)
{
cout<<"打开文件失败!"<<endl;
return NULL;
}
m_Ifile.seekg(0,ios_base::end); //将文…
[/Quote]
这个方法可用,我想问问我的是哪出问题了?
chin_chen 2009-03-17
  • 打赏
  • 举报
回复
不是你链表的问题,我是说你那个
 while(getline(line,m_Ifile))                                        //读取学生信息
{
os<<line;
pNew=new Node();
os>>pNew->sID;
os>>pNew->sName;
os>>pNew->bSex;
os>>pNew->Score[0];
os>>pNew->Score[1];
if(pHead==NULL)
{
pTail=pHead=pNew;
}
else
{
pTail->pNext=pNew;
pTail=pTail->pNext;
}
pTail->pNext=NULL;

}

这个设计可能有逻辑错误 。

你那个pTail,pHead,pNext分别怎么定义的哦,
smdszgzh 2009-03-17
  • 打赏
  • 举报
回复
如果我不从文件里读内容,直接添加学生是信息然后输出,就不会出现这样的情况!所以我觉得应该不是链表输出的问题!
chin_chen 2009-03-17
  • 打赏
  • 举报
回复
[Quote=引用楼主 smdszgzh 的帖子:]
C/C++ codeNode*StudentList::ReadFile(char*filename)
{
ifstream m_Ifile;
m_Ifile.open(filename,ios::in);stringline,word;if(!m_Ifile)
{
cout<<"打开文件失败!"<<endl;returnNULL;
}
m_Ifile.seekg(0,ios_base::end);//将文件指针移动到文件尾部if(!m_Ifile.tellg())//文件长度为0,返回{
cout<<"不存在任何学生信息,请添加!"<<endl;returnNULL;
}
m_Ifile.seekg(0);//将文件…
[/Quote]
是你的链表那个
if(pHead==NULL)
{
pTail=pHead=pNew;
}
else
{
pTail->pNext=pNew;
pTail=pTail->pNext;
}
pTail->pNext=NULL;

有问题吧。
tangshuiling 2009-03-17
  • 打赏
  • 举报
回复

把while(!m_Ifile.eof()) 换成while(getline(line,m_Ifile))的形式试试
Node* StudentList::ReadFile(char *filename)
{
ifstream m_Ifile;
m_Ifile.open(filename,ios::in);
string line,word;
stringstream os;
if(!m_Ifile)
{
cout<<"打开文件失败!"<<endl;
return NULL;
}
m_Ifile.seekg(0,ios_base::end); //将文件指针移动到文件尾部
if(!m_Ifile.tellg()) //文件长度为0,返回
{
cout<<"不存在任何学生信息,请添加!"<<endl;
return NULL;
}
m_Ifile.seekg(0); //将文件指定重新移动到首位置
while(getline(line,m_Ifile)) //读取学生信息
{
os<<line;
pNew=new Node();
os>>pNew->sID;
os>>pNew->sName;
os>>pNew->bSex;
os>>pNew->Score[0];
os>>pNew->Score[1];
if(pHead==NULL)
{
pTail=pHead=pNew;
}
else
{
pTail->pNext=pNew;
pTail=pTail->pNext;
}
pTail->pNext=NULL;

}
cout<<"加载学生信息成功!"<<endl;
return pHead;
}

tangshuiling 2009-03-17
  • 打赏
  • 举报
回复

chin_chen 把你的char ch[156];改为string ch;你就清楚了!
string类型不会接受行尾的回车符,char数组只要是字符通入囊中!
feng4206yu 2009-03-17
  • 打赏
  • 举报
回复
自己做了些试验,不好说清楚....还是用getline()保险一点....
chin_chen 2009-03-17
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 tangshuiling 的回复:]
C/C++ code话不多说了,如果楼上的各位还没有理解file.eof()的精髓,看看下面间单的实例,或许对理解有帮助!int_tmain(intargc, _TCHAR*argv[])
{
ifstream infile("c:\\1.txt");//此文件中存有字串abcstringch;while(!infile.eof())
{
infile>>ch;
cout<<ch;//这里会输出两次abcabc, why?}return0;
}
结合我上面说的,慢慢体会吧,自己琢磨出来的比别人讲的更利于记忆!
[/Quote]
我的代码是这样的
#include <iostream>
#include <fstream>
using namespace std;




int main()
{



ifstream infile("test.txt"); //此文件中存有字串abc
char ch[156];
while(!infile.eof())
{
infile>>ch;
cout<<ch; //这里会输出两次abcabc, why?
}


system("pause");

return 0;
}


结果只是一个abc,哪位是2个abc的举个手看下!
feng4206yu 2009-03-17
  • 打赏
  • 举报
回复
如果文件是先利用程序写入后读取的,那么有可能是写入时的分隔符造成了所谓的9楼所说的错误...但是如果是手工书写的文件,应该不会出现9楼所说的错误的...
fengfan 2009-03-17
  • 打赏
  • 举报
回复
eof 应该没有问题吧.是不是文件格式的问题??
tangshuiling 2009-03-17
  • 打赏
  • 举报
回复

话不多说了,如果楼上的各位还没有理解file.eof()的精髓,看看下面间单的实例,或许对理解有帮助!
int _tmain(int argc, _TCHAR* argv[])
{
ifstream infile("c:\\1.txt"); //此文件中存有字串abc
string ch;
while(!infile.eof())
{
infile>>ch;
cout<<ch; //这里会输出两次abcabc, why?
}
return 0;
}
结合我上面说的,慢慢体会吧,自己琢磨出来的比别人讲的更利于记忆!

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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