我想交替读取多个文件。下面是我的代码的简化版本。
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
Run Code Online (Sandbox Code Playgroud)
如果我一次阅读这些文件并且我认为一切都很酷,那么该代码运行良好。但是,如果称为“循环部分”的部分位于循环内,则行为会变得很奇怪,并且我开始有一个重复输出。请问,有人可以告诉我出了什么问题以及如何解决它吗?如果您有更好的解决问题的方法,请提出。我很感激。
//解决了
谢谢大家的评论,我很感激。这是我简单做的:
而不是原来的
switch (ID)
{
case 0:
In_file = In_file2; …Run Code Online (Sandbox Code Playgroud)