Please will you and everyone else note that the correct way to read a text file does NOT require the use of the eof(), good(), bad() or indifferent() functions (OK, I made the last one up). The same is true in C (with fgets(), feof() et al). Basically, these flags will only be set AFTER you have attempted to read something, with a function like getline(). It is much simpler and more likely to be correct to test that read functions, like getline() have actually read something directly.
Not tested - I'm upgrading my compiler:
#include <iostream>
#include <fstream>
#include <string>
using namespacr std;
imt main() {
string filename;
getline( cin, filename );
ifstream ifs( filename.c_str() );
if ( ! ifs.is_open() ) {
// error
}
string line;
while( getline( ifs, line ) ) {
// do something with line
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…