У меня есть фрагмент кода c ++, который должен читать некоторую информацию из файла и не найти файл или информацию, прочитать его с экрана. Вот код:
char c;
bool found=false;
int N;
double step;
ifstream in;
in.open(name.c_str());
if(in) {
while(!found && in >> c) {
while(c!='=') in >> c;
in >> N;
if(N==wp.N) {
found=true;
while(c!=':') in >> c;
in >> step;
}
c='a';
}
}
if(!found) {
cout << "max=";
cin >> step;
}
Вышеупомянутое построено в соответствии со структурой данных в файле, и все работает, когда информация находится в файле (т. Е. N = wp.N в какой-то момент), или файл не существует (т.е. , если (in) неверно).
But the problem arises when the file exist but doesn't contain the info, that is N==wp.N is never true. Then the program freezes (presumably at the end of the file, so that found is never true). I was expecting that including in >> c inside the while-loop would fix this, but I've also tried using in.eof().
Какие-либо предложения?