我想在讀取多個文件之間進行切換。以下是我的代碼的簡化版本。讀取多個文件
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