[cfe-dev] Premature EOF when reading binary file
speter at inf.ethz.ch
Wed Feb 2 07:03:46 PST 2011
I'm using libc++ inside the Barrelfish operating system, compiled
using g++ 4.4.5.
When reading a binary file using the read() method on a regular fstream
object, I sometimes get an EOF prematurely, e.g.:
uint64_t actual_size = 1234;
char *data = (char*) malloc(actual_size);
fstream binary_file(file_path, ios::in|ios::binary);
This won't read the complete file into the buffer and sets the EOF bit
on the stream. The file is longer than 1234 bytes.
I tracked the problem down to the implicit cast in fstream, line 582:
__c = *this->gptr();
In this case, this->gptr() returns a 'char *' to the next available
character in the input stream, while __c is an 'int'. The implicit cast
ends up converting the input character to -1 (a valid character in the
input stream), which coincides with traits_type::eof() and is thus
treated as an end-of-file indication.
I am unsure how to fix this and would be grateful for suggestions.
More information about the cfe-dev