重複の可能性:
C++ のファイルの終わり
.txt ファイルからデータを読み取る関数を作成しましたが、呼び出すとクラッシュし続けます。コードは次のとおりです。
void beolvas(vector<int> &charge, vector<int> &deliver, vector<Robot*> &robots) {
string s;
ifstream f;
do {
cout << "Add meg a filenevet" << endl;
cin >> s;
f.open(s.c_str());
} while (!f.good());
cout << "adatok beolvasasa..." << endl;
int napok;
if (!(f >> napok)) throw 1;
charge.resize(napok);
deliver.resize(napok);
for (int i = 0; i<napok; i++) {
if (!(f>>charge[i])) throw 1;
if (!(f>>deliver[i])) throw 1;
}
string type, name;
int battery;
while (!f.eof()) {
cout << " a ";
if (f>>type && f>>name && f>>battery) {
if (type=="Mac") {
Mac r = Mac(name,battery);
robots.push_back(&r);
};
if (type=="Eco") {
Eco r = Eco(name,battery);
robots.push_back(&r);
};
if (type=="Pro") {
Pro r = Pro(name,battery);
robots.push_back(&r);
};
};
};
}
whileループで問題が発生しているようです。3行の長いテキストから読みたい場合、画面のように4文字になります(すべての行を読む前にプログラムに1つ印刷させます)。
f.eof()
ここで使用する必要がある関数ではありませんか?