65,211
社区成员
发帖
与我相关
我的任务
分享
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;
}
m_Ifile.seekg (0, ios_base::end);
int length = m_Ifile.tellg(); // length 为0则文件为空
if(!m_Ifile)
{
cout<<"打开文件失败!"<<endl;
return NULL;
}