Why is (foobar>>x) preferred over (! foobar.eof() ) [duplicate]
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Why is iostream::eof inside a loop condition considered wrong? eof() bad practice? My teacher said we shouldn't use EOF to read in text file or binary file information instead we should use (afile>>x). He didn't explain why, can someone explain to me. Can someone also explain what are the differences in this two different method of reading //Assuming declaration //ifstream foobar ( ! foobar.eof() ) { foobar>>x;