std::basic_istream<CharT,Traits>::read (3) - Linux Manuals
std::basic_istream<CharT,Traits>::read: std::basic_istream<CharT,Traits>::read
Command to display std::basic_istream<CharT,Traits>::read
manual in Linux: $ man 3 std::basic_istream<CharT,Traits>::read
NAME
std::basic_istream<CharT,Traits>::read - std::basic_istream<CharT,Traits>::read
Synopsis
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). The number of successfully extracted characters can be queried using gcount().
Parameters
s - pointer to the character array to store the characters to
count - number of characters to read
Return value
*this
Exceptions
failure if an error occurred (the error state flag is not goodbit) and exceptions() is set to throw for that state.
If an internal operation throws an exception, it is caught and badbit is set. If exceptions() is set for badbit, the exception is rethrown.
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
// Run this code
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdint>
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';
}
}
Output:
0x12121212
abcd1
abcd2
abcd3
See also
inserts blocks of characters
write (public member function of std::basic_ostream<CharT,Traits>)
extracts formatted data
operator>> (public member function)
extracts already available blocks of characters
readsome (public member function)
extracts characters
get (public member function)
extracts characters until the given character is found
getline (public member function)
reads from a file
fread (function)
Pages related to std::basic_istream<CharT,Traits>::read
- std::basic_istream<CharT,Traits>::readsome (3) - std::basic_istream<CharT,Traits>::readsome
- std::basic_istream<CharT,Traits>::basic_istream (3) - std::basic_istream<CharT,Traits>::basic_istream
- std::basic_istream<CharT,Traits>::gcount (3) - std::basic_istream<CharT,Traits>::gcount
- std::basic_istream<CharT,Traits>::get (3) - std::basic_istream<CharT,Traits>::get
- std::basic_istream<CharT,Traits>::getline (3) - std::basic_istream<CharT,Traits>::getline
- std::basic_istream<CharT,Traits>::ignore (3) - std::basic_istream<CharT,Traits>::ignore
- std::basic_istream<CharT,Traits>::operator= (3) - std::basic_istream<CharT,Traits>::operator=
- std::basic_istream<CharT,Traits>::operator>> (3) - std::basic_istream<CharT,Traits>::operator>>
- std::basic_istream<CharT,Traits>::peek (3) - std::basic_istream<CharT,Traits>::peek
- std::basic_istream<CharT,Traits>::putback (3) - std::basic_istream<CharT,Traits>::putback
- std::basic_istream<CharT,Traits>::seekg (3) - std::basic_istream<CharT,Traits>::seekg
- std::basic_istream<CharT,Traits>::sentry (3) - std::basic_istream<CharT,Traits>::sentry
- std::basic_istream<CharT,Traits>::swap (3) - std::basic_istream<CharT,Traits>::swap
- std::basic_istream<CharT,Traits>::sync (3) - std::basic_istream<CharT,Traits>::sync
- std::basic_istream<CharT,Traits>::tellg (3) - std::basic_istream<CharT,Traits>::tellg
- std::basic_istream<CharT,Traits>::unget (3) - std::basic_istream<CharT,Traits>::unget
- std::basic_istream<CharT,Traits>::~basic_istream (3) - std::basic_istream<CharT,Traits>::~basic_istream
- std::basic_istream (3) - std::basic_istream
- std::basic_istream_sentry (3)
- std::basic_istringstream (3) - std::basic_istringstream