同一个ifstream流打开不同文件问题
如果想要同一个ifstream流打开不同的文件,除了必须先关闭第一个打开的文件外,更重要的是使用clear()函数清除所有的标志.代码如下
void readInfo()
{
// 将文件中的房间和学生信息存入变量
ifstream in;
int roomNo , capacity, count;
char sex;
string phone;
in.open("roomInfo.dat");
if(!in)
{
cerr<<"读房间信息失败, 请确定文件存在!"<<endl;
exit(0);
}
while(!in.eof())
{
in>>roomNo>>sex>>phone>>capacity>>count; // 将信息读入变量
roomsInfo.push_back(new room(roomNo, sex, phone, capacity, count));
}
in.close(); // 关闭文件
in.clear(); // 必须清除所有的状态标志
string sNo, name, sClass, tm;
in.open("stuInfo.dat");
if(!in)
{
cerr<<"读学生信息失败, 请确定文件存在!"<<endl;
exit(0);
}
while(!in.eof())
{
in>>sNo>>name>>sex>>sClass>>phone>>tm>>roomNo;
stuInfo.push_back(new student(name, sex, sClass, sNo, phone, tm, roomNo));
}
in.close();
}// readInfo

浙公网安备 33010602011771号