std::basic_istream<CharT,Traits>:: read
|
basic_istream
&
read
(
char_type
*
s,
std::
streamsize
count
)
;
|
||
스트림에서 문자를 추출합니다.
UnformattedInputFunction 처럼 동작합니다. sentry 객체를 구성하고 검사한 후, 문자를 추출하여 s 가 가리키는 첫 번째 요소부터 시작하는 문자 배열의 연속된 위치에 저장합니다. 다음 조건 중 하나가 발생할 때까지 문자를 추출하고 저장합니다:
- count 개의 문자가 추출되어 저장되었습니다.
- 입력 시퀀스에서 파일 끝 조건이 발생하는 경우(이 경우, setstate ( failbit | eofbit ) 가 호출됨). 성공적으로 추출된 문자 수는 gcount() 를 사용하여 조회할 수 있습니다.
목차 |
매개변수
| s | - | 문자를 저장할 문자 배열에 대한 포인터 |
| count | - | 읽을 문자 수 |
반환값
* this
예외
내부 연산에서 예외가 발생하면, 해당 예외는 catch되어
badbit
이 설정됩니다. 만약
exceptions()
이
badbit
에 대해 설정되어 있다면, 예외가 다시 throw됩니다.
참고 사항
비변환 로케일(기본 로케일은 비변환입니다)을 사용할 때, std::basic_ifstream 에서 이 함수의 오버라이더는 제로-카피 벌크 I/O를 위해 최적화될 수 있습니다( std::streambuf::xsgetn 을 오버라이딩하는 방식으로).
예제
#include <cstdint> #include <fstream> #include <iostream> #include <sstream> #include <string> int main() { // read()는 주로 바이너리 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'; // 다음 코드 조각을 위한 파일 준비 std::ofstream("test.txt", std::ios::binary) << "abcd1\nabcd2\nabcd3"; // 전체 파일을 문자열로 읽기 if (std::ifstream is{"test.txt", std::ios::binary | std::ios::ate}) { auto size = is.tellg(); std::string str(size, '\0'); // 스트림 크기에 맞게 문자열 구성 is.seekg(0); if (is.read(&str[0], size)) std::cout << str << '\n'; } }
출력:
0x12121212 abcd1 abcd2 abcd3
참고 항목
|
문자 블록을 삽입합니다
(
std::basic_ostream<CharT,Traits>
의
public member function)
|
|
|
형식화된 데이터를 추출합니다
(public member function) |
|
|
이미 사용 가능한 문자 블록을 추출합니다
(public member function) |
|
|
문자를 추출합니다
(public member function) |
|
|
주어진 문자를 찾을 때까지 문자를 추출합니다
(public member function) |
|
|
파일에서 읽습니다
(function) |