가능한 중복 :
End of File in C++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 자의 a-s가 표시됩니다 (모든 행을 읽으려면 프로그램에서 인쇄합니다).
f.eof()
은 내가 여기서 사용해야하는 기능이 아닙니까?
'eof()'는 항상 잘못되었습니다. SO에는 수백 개의 복제본이 있습니다. –