c++ - File Stream failing on read after seeking to end of file -
i need read last 64kb large file, after seeking end of file, 64kb, reads on file fail.
here's snippet:
string filename = "test.txt"; ifstream::streampos pos; // used keep track of position in file ifstream fs; fs.open(filename, ios::binary); fs.seekg(0, ios::end); pos = fs.tellg(); // .... fs.seekg(-16, ios::cur); char buff[16]; fs.read(buff, 16); // ... fs.close();
the problem on fs.read()
eofbit
, failbit
, , badbit
set , file stream becomes unusable. worth noting file i'm reading being continuously appended to. also, approach works , doesn't, makes debugging difficult.
is there reason why happening?
your problem call tellg
after seeking end of file. tellg
causes pointer advance invoking eof state on stream: here issue:
fs.open(filename, ios::binary); fs.seekg(0, ios::end); pos = fs.tellg(); // <== causes eof on stream
your next call fs.seekg
should clear eof state, better call fs.clear()
, check return insure succeeds.
Comments
Post a Comment