Reputation: 11
void foo() {
ifstream stream;
stream.open(name);
if(stream.fail()){
cout << "invalid";
return;
}
int p;
int q;
string id;
int r;
int s;
stream >> id >> r >>s;
while( !stream.eof() ) {
char f[3];
char w[3];
char j[3];
stream.getline(f,3);
stream.getline(w,3);
stream.getline(j,3);
stream >> p;
stream >> q;
}
stream.close();
}
this code is meant to read the first line of a file and save data from there. Then it is meant to save every 3 characters into a separate char array that will later be saved into a member variable on an object that hasn't been impleneted yet. However, when i run it, instead of reading anything, the stream seems to fail and cause an infinite loop. I am pretty sure the file has valid data.
Upvotes: 0
Views: 77
Reputation: 249113
Your loop condition is wrong:
while( !stream.eof() ) {
It should be:
while( stream ) {
In particular, your code is looping infinitely when an input operation fails, because you never reach EOF.
Upvotes: 1