| I/O manipulators | ||||
| Print functions(C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
| Synchronized Output | ||||
(C++20) | ||||
| Types | ||||
| Error category interface | ||||
(C++11) | ||||
(C++11) |
| Global objects | ||||
| Member functions | ||||
(C++11) | ||||
| Formatted input | ||||
| Unformatted input | ||||
basic_istream::read | ||||
| Positioning | ||||
| Miscellaneous | ||||
(C++11) | ||||
| Member classes | ||||
| Non-member functions | ||||
basic_istream& read( char_type* s,std::streamsize count); | ||
Extracts characters from stream.
Behaves asUnformattedInputFunction. 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 bys. Characters are extracted and stored until any of the following conditions occurs:
Contents |
| s | - | pointer to the character array to store the characters to |
| count | - | number of characters to read |
*this
If an internal operation throws an exception, it is caught andbadbit is set. Ifexceptions() is set forbadbit, the exception is rethrown.
When using a non-converting locale (the default locale is non-converting), the overrider of this function instd::basic_ifstream may be optimized for zero-copy bulk I/O (by means of overridingstd::streambuf::xsgetn).
#include <cstdint>#include <fstream>#include <iostream>#include <sstream>#include <string> int main(){// read() is often used for binary I/Ostd::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 snippetstd::ofstream("test.txt", std::ios::binary)<<"abcd1\nabcd2\nabcd3"; // read entire file into stringif(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:
0x12121212abcd1abcd2abcd3
| inserts blocks of characters (public member function of std::basic_ostream<CharT,Traits>)[edit] | |
| extracts formatted data (public member function)[edit] | |
| extracts already available blocks of characters (public member function)[edit] | |
| extracts characters (public member function)[edit] | |
| extracts characters until the given character is found (public member function)[edit] | |
| reads from a file (function)[edit] |