我想在读取多个文件之间进行切换。以下是我的代码的简化版本。读取多个文件
ifstream* In_file1 = new ifstream("a.dat", ios::binary);
ifstream* In_file2 = new ifstream("b..dat", ios::binary);
ifstream* In_file;
int ID;
In_file = In_file1;
int ID = 0;
//LOOPING PORTION
if (In_file -> eof())
{
In_file -> seekg(0, ios_base::beg);
In_file->close();
switch (ID)
{
case 0:
In_file = In_file2; ID = 1; break;
case 1:
In_file = In_file1; ID = 0; break;
}
}
//some codes
:
:
In_file->read (data, sizeof(double));
//LOOPING PORTION
代码工作得很好,如果我读文件一次,我认为一切都很酷。但是,如果称为“循环部分”的部分位于循环内部,则行为会变得很奇怪,我开始有一个单独的重复输出。请,有人可以告诉我什么是错的,我该如何解决它?如果你有更好的方法来解决这个问题,请建议。我很感激。
//解决
谢谢大家对你的意见,我很感激。这是我简单做:
而不是原来的
switch (ID)
{
case 0:
In_file = In_file2; ID = 1; break;
case 1:
In_file = In_file1; ID = 0; break;
}
我压根儿
switch (ID)
{
case 0:
In_file = new ifstream("a.dat", ios::binary); ID = 1; break;
case 1:
In_file = new ifstream("b.dat", ios::binary); ID = 0; break;
}
现在它就像魅力,我可以循环,就像我想:-)。我很感谢你的评论,很高兴认识大哥还有帮助。
时都处于EOF会发生什么? – littleadv 2011-06-10 08:34:38
不应该在分配InFile后检查InFile-> eof()吗? – 2011-06-10 08:43:31
'如果被称为“循环部分”的部分在循环内......例如? – 2011-06-10 08:46:16