ファイルから文字ごとに入力を受け取る関数があります。
#include <iostream>
#include <fstream>
using namespace std;
ifstream input("sequence.txt");
char getChar(){
char nextType;
if (input.eof()) {
input.clear();
input.seekg(0,ios::beg);
}
input >> nextType;
return nextType;
}
int main(){
for(int i = 0; i < 10; i++){
cout << getChar() << endl;
}
return 0;
}
「sequence.txt」内の入力は次のとおりです。
I O
したがって、出力には I と O が交互に表示されますが、代わりに次のように出力されます。
I O O I O O I O O I
ファイルの最後の文字を最初に読み取った後、ファイルをリセットするにはどうすればよいですか?