jeudi 24 septembre 2015

Parse std::uint types using stream manipulators in C++

I am trying to parse a file using stream manipulators. However, as the operator>> overload for std::uint8_t, std::uint16_t, std::uint32_t from <istreams> is a formatted input function, it reads a string and if necessary converts it to a number. SO it would parse 23 to 23 and not the binary representation 00000010 00000011 to 515.

You can do this using the inputstream.read() method with a reinterpret_cast<>:

is.read(reinterpret_cast<char*>(data), sizeof(*data));

Now my question - Is it possible to implement to overwrite the default behavior of the >> operator so I can read in such std:uint types using the >> operator? How would I implement it and how can I switch between the operators? And if it is not possible do you know a nice way to implement a stream manipulator to do the parsing?

Aucun commentaire:

Enregistrer un commentaire