Reading binary istream byte by byte

前端 未结 5 615
迷失自我
迷失自我 2021-01-01 10:57

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

相关标签:
5条回答
  • 2021-01-01 11:10
    source.get()
    

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

    0 讨论(0)
  • 2021-01-01 11:23

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

    0 讨论(0)
  • 2021-01-01 11:24

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

    0 讨论(0)
  • 2021-01-01 11:25

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

    0 讨论(0)
  • 2021-01-01 11:30

    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.

    0 讨论(0)
提交回复
热议问题