Coming back to sort of play around with C++ a little bit after some years out of college, when looking up how to read a file as bytes in C++, some of the information I came across is that there isn't any sort of magical "readAsBytes" function, and you essentially are supposed to do this by reading a file the same way you would a text file, but with making sure to store the results into a char*
. For instance:
someIFStream.read(someCharPointer, sizeOfSomeCharPointer);
That being said, even though chars
in C++ are usually supposed to be right around 8 bits, this isn't exactly guaranteed. Start messing around with different platforms and text encodings long enough, and you're going to run into issues if you want a true array of bytes.
You could just use a uint8_t*
and copy everything over from the char*
. . . but dang, that's wasteful. Why can't we just get everything into a uint8_t*
the first time around, while we're still reading the file, in a way that doesn't have to worry about whether it's a 32-bit machine or a 64-bit machine or UTF-8 or UTF-16 or what have you?
So the question is: Is this possible, at least in more modern C++ versions? If so, how? The reason I don't want to go from a char*
to a uint8_t*
is basically one of not having to waste a bunch of CPU cycles on some 50,000-iteration for
loop. Thanks!
EDIT
I'm defining a byte as 8 bits for the purposes of this question, unless somebody strongly suggests otherwise. My understanding is that bytes were originally 6 bits, then became 7, and then finally settled down on 8, but that 32-bit groupings and such are usually thought of as small collections of bytes. If I'm mistaken, or if I should think of this problem differently (either way), please bring it up.