Namespaces
Variants
Views
Actions

std::basic_istream::read

From cppreference.com
< cpp‎ | io‎ | basic istream
Revision as of 18:29, 31 March 2013 by SuperBoi45 (Talk | contribs)

basic_istream& read( char_type* s, std::streamsize count );

Extracts characters from stream.

Behaves as UnformattedInputFunction. After constructing and checking the sentry object, 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

Parameters

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

Return value

*this

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), sizeof n);
    std::cout << std::hex << std::showbase << n << '\n';
}

Output:

0x12121212

See also

Template:cpp/io/basic ostream/dcl list writeTemplate:cpp/io/basic istream/dcl list operator gtgtTemplate:cpp/io/basic istream/dcl list readsomeTemplate:cpp/io/basic istream/dcl list getTemplate:cpp/io/basic istream/dcl list getlineTemplate:cpp/io/c/dcl list fread