I was attempting to read a binary file byte by byte using an ifstream. I\'ve used istream methods like get() before to read entire chunks of a binary file at once without a
source.get()
will give you a single byte. It is unformatted input function. operator>> is formatted input function that may imply skipping whitespace characters.
Why are you using formatted extraction, rather than .read()
?
there is a read() member function in which you can specify the number of bytes.
As others mentioned, you should use istream::read(). But, if you must use formatted extraction, consider std::noskipws.
The >> extractors are for formatted input; they skip white space (by
default). For single character unformatted input, you can use
istream::get() (returns an int
, either EOF if the read fails, or
a value in the range [0,UCHAR_MAX]) or istream::get(char&)
(puts the
character read in the argument, returns something which converts to
bool
, true if the read succeeds, and false if it fails.