Why is failbit () installed?

Create a file and fill it with zeros:

dd if=/dev/zero of=/tmp/zeroes count=1

Write this little program to extract the first unsigned integer that it encounters in the file.

#include <assert.h>
#include <fstream>

int main()
{
    std::ifstream reader( "/tmp/zeroes", std::ios_base::binary );
    uint32_t number;
    reader >> number;

    assert( !reader.fail() );
}

Why is assert initiated?

+4
source share
1 answer

Because it /dev/zerosupplies binary zeros, not a character '0', but >>performs (or tries to) convert from text.

+8
source

Source: https://habr.com/ru/post/1535926/


All Articles