判断文件为空问题

smdszgzh 2009-03-17 09:38:44

Node* StudentList::ReadFile(char *p)
{
ifstream m_Ifile;
m_Ifile.open(p,ios::in);
string line,word;
if(!m_Ifile)
{
cout<<"打开文件失败!"<<endl;
return NULL;
}
if(m_Ifile.eof())//这里不管文件是不是空,都返回FALSE,为什么??如果判断文件是否为空?
{
cout<<"不存在任何学生信息,请添加!"<<endl;
return NULL;
}
do
{
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;

}while(!m_Ifile.eof());
cout<<"加载学生信息成功!"<<endl;
return pHead;

}

...全文
166 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangyadong 2009-03-17
  • 打赏
  • 举报
回复
你他妈鄙视谁啊 我又没看到你发,垃圾!
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 smdszgzh 的回复:]
不好意思,我想再问问,空文件没有设计文件结尾标志吗,如果设置了不是第一个吗?
[/Quote]

打开文件以后, 没有读之前是不会 EOF 的
chin_chen 2009-03-17
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 smdszgzh 的回复:]
不好意思,我想再问问,空文件没有设计文件结尾标志吗,如果设置了不是第一个吗?
[/Quote]
这个情况来看,貌似是没有的。
smdszgzh 2009-03-17
  • 打赏
  • 举报
回复
不好意思,我想再问问,空文件没有设计文件结尾标志吗,如果设置了不是第一个吗?
chin_chen 2009-03-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wangyadong 的回复:]
m_Ifile.seekg  (0,  ios_base::end);
int  length  =  m_Ifile.tellg();  //  length  为0则文件为空
[/Quote]
鄙视你这种人,bs!!!!
wangyadong 2009-03-17
  • 打赏
  • 举报
回复
m_Ifile.seekg (0, ios_base::end);
int length = m_Ifile.tellg(); // length 为0则文件为空
chin_chen 2009-03-17
  • 打赏
  • 举报
回复
这样吧

m_Ifile.seekg (0, ios_base::end);
int length = m_Ifile.tellg(); // length 为0则文件为空
if(!m_Ifile)
{
cout<<"打开文件失败!"<<endl;

return NULL;
}
chin_chen 2009-03-17
  • 打赏
  • 举报
回复
你看看这个m_Ifile.eof()是个怎么回事吧,先

bool eof ( ) const;

Check if eofbit is set

The function returns true if the eofbit stream's error flag has been set by a previous i/o operation. This flag is set by all standard input operations when the End Of File is reached in the sequence associated with the stream.

65,211

社区成员

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

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