std::basic_istream::read

From cppreference.com
basic_istream& read( char_type* s, std::streamsize count );

Extracts characters from stream.

First, constructs a std::basic_istream::sentry object with noskipws set to true. Afterwards, if good()==false, calls setstate(failbit) and returns. Otherwise, extracts characters and stores them into successive locations of the character array whose first element is pointed to by s. Characters are extracted and stored until any of the following conditions occurs:

  • count characters were extracted and stored
  • end of file condition occurs on the input sequence (in which case, setstate(failbit|eofbit) is called.

Contents

[edit] Parameters

s - pointer to the character array to store the characters to
count - number of characters to read

[edit] Return value

*this

[edit] Example

read() is often used for binary I/O

#include <iostream>
#include <string>
#include <sstream>
#include <cstdint>
int main()
{
    std::string bin = {'\x12', '\x12', '\x12', '\x12'};
    std::istringstream raw(bin);
    std::uint32_t n;
    raw.read(reinterpret_cast<char*>(&n), 4);
    std::cout << std::hex << std::showbase << n << '\n';
}

Output:

0x12121212

[edit] See also

inserts blocks of characters
(public member function of std::basic_ostream)
extracts formatted data
(public member function)
extracts already available blocks of characters
(public member function)
extracts characters
(public member function)
extracts characters until the given character is found
(public member function)
reads from a file
(function)