Reading binary istream byte by byte

北慕城南 提交于 2019-11-30 17:09:18

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.

there is a read() member function in which you can specify the number of bytes.

Why are you using formatted extraction, rather than .read()?

source.get()

will give you a single byte. It is unformatted input function. operator>> is formatted input function that may imply skipping whitespace characters.

As others mentioned, you should use istream::read(). But, if you must use formatted extraction, consider std::noskipws.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!