std::basic_istream<CharT,Traits>::read
Min standard notice:
Extracts characters from stream.
# Declarations
basic_istream& read( char_type* s, std::streamsize count );
# Parameters
s: pointer to the character array to store the characters tocount: number of characters to read
# Return value
*this
# Notes
When using a non-converting locale (the default locale is non-converting), the overrider of this function in std::basic_ifstream may be optimized for zero-copy bulk I/O (by means of overriding std::streambuf::xsgetn).
# Example
#include <cstdint>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
int main()
{
// read() is often used for binary I/O
std::string bin = {'\x12', '\x12', '\x12', '\x12'};
std::istringstream raw(bin);
std::uint32_t n;
if (raw.read(reinterpret_cast<char*>(&n), sizeof n))
std::cout << std::hex << std::showbase << n << '\n';
// prepare file for next snippet
std::ofstream("test.txt", std::ios::binary) << "abcd1\nabcd2\nabcd3";
// read entire file into string
if (std::ifstream is{"test.txt", std::ios::binary | std::ios::ate})
{
auto size = is.tellg();
std::string str(size, '\0'); // construct string to stream size
is.seekg(0);
if (is.read(&str[0], size))
std::cout << str << '\n';
}
}