[cfe-dev] Premature EOF when reading binary file
hhinnant at apple.com
Wed Feb 2 09:41:19 PST 2011
On Feb 2, 2011, at 10:47 AM, Simon Peter wrote:
> 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);
> binary_file.read(data, actual_size);
> 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.
Thanks Simon. I've committed a fix to revision 124727. Please let me know if that did not clear up the problem.
More information about the cfe-dev