c++ - eof() bad practice? -
possible duplicate:
why iostream::eof inside loop condition considered wrong?
so i've been using eof()
function in lot of programs require file input, , professor said fine use few people on have said shouldn't use without specifying reason. wondering, there reason?
you can use eof
test exact condition reports - whether have attempted read past end of file. cannot use test whether there's more input read, or whether reading succeeded, more common tests.
wrong:
while (!cin.eof()) { cin >> foo; }
correct:
if (!(cin >> foo)) { if (cin.eof()) { cout << "read failed due eof\n"; } else { cout << "read failed due other eof\n"; } }
Comments
Post a Comment