io - c++ eof and failbit in the middle of reading binary file -
i have troubles reading binary file. seems not read end:
// file size ifs.open (infile.c_str(), ios::binary | ios::ate); cout << "file size: " << ifs.tellg() << endl;; ifs.close(); // read file ifs.open (infile.c_str(), ios::in | ios::binary); int counter = 0; char c = 0; (counter = 0; ifs; ++counter) ifs >> c; cout << "last char: " << int(c) << endl; cout << "read bytes: " << counter << endl; cout << "fail? " << (ifs.fail() ? "yes" : "no") << endl; cout << "bad? " << (ifs.bad() ? "yes" : "no") << endl; cout << "eof? " << (ifs.eof() ? "yes" : "no") << endl; ifs.close();
below output. don't understand why eofbit
in middle of file , why comes failbit
:
file size: 289384 last char: 1 read bytes: 288598 fail? yes bad? no eof? yes
i on unix system
i did test , discovered problem. it's obvious once add ofs << c
loop.
it not reading white-space.
you can fix either adding #include <iomanip>
, ifs >> noskipws
, or using binary input functions ifs.get(c)
Comments
Post a Comment