std::basic_istream::read(3)
πΈ NAME πΈ
okay bestie, this is the std::basic_istream::read(3) page from the C++ Standard Libary manual π. The NAME is literally std::basic_istream::read β thatβs the whole title, no cap.
β¨ 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:
countcharacters 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 usinggcount().
π Parameters π
sβ pointer to the character array to store the characters tocountβ 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, bestie:
// Run this code
#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';
}
}
π Output π
0x12121212
abcd1
abcd2
abcd3
π See also π
writeβ inserts blocks of characters (public member function ofstd::basic_ostream<CharT,Traits>)operator>>β extracts formatted data (public member function)readsomeβ extracts already available blocks of characters (public member function)getβ extracts characters (public member function)getlineβ extracts characters until the given character is found (public member function)freadβ reads from a file (function)
okay thatβs the whole page, bestie β source/version is http://cppreference.com, and the date is 2024.06.10 π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms