Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when use fstream in cplusplus, what's wrong with this program

Tags:

c++

The next program what's wrong? I prefer the file reach the end.

ifstream file("main.cpp", ios::binary | ios::ate);
if (file) {
    //fstream::pos_type size = file.tellg();
    file.seekg(100, fstream::cur);
    if (file.eof()) {
        cout << "eof is true\n";
    }
}

fstream reach the end of the file, but why not echo "eof is true".

like image 453
minji_LT Avatar asked Mar 25 '23 02:03

minji_LT


1 Answers

The eof bit is only set (and thus eof() only returns true) if an actual read operation fails due to hitting the end of the file. A seek operation (apparently) isn't enough.

like image 164
Sebastian Redl Avatar answered Apr 06 '23 07:04

Sebastian Redl