c++c++11binaryistream

C++11: istream.get() stops on EOM


I have a problem with C++ (C++11) inputstreams, specifically filestreams. After I open a new stream with this:

ifstream stream;
stream.open("C:\somefile.txt");

And when I try to read from it and the read operation encounters an EOM-byte (0x19), the stream is set to badbit which is not what I want. I want to read until the definite end of the file. This is how I read

char buffer[8];
stream.read(buffer, 8);

and then I check like that:

if(stream.fail()) return -1;

How can I read from the stream without stopping at EOM-bytes (or equal)?


Solution

  • Open the file as a binary stream instead:

    ifstream stream{ "C:\\somefile.txt", std::ios::binary };
    

    This should cause the stream to ignore the value of individual bytes, on reading (and simply read as a block).