Namespaces
Variants

std::strstreambuf:: seekpos

From cppreference.net
protected :

virtual pos_type seekpos ( pos_type sp,
std:: ios_base :: openmode which =

std:: ios_base :: in | std:: ios_base :: out ) ;
(C++98에서 사용 중단됨)
(C++26에서 제거됨)

가능한 경우 std::basic_streambuf::gptr 및/또는 std::basic_streambuf::pptr sp 가 가리키는 위치로 재배치합니다.

만약 std::ios_base::in which 에 설정되어 있으면, gptr() (get 영역의 다음 포인터)의 위치 재조정을 시도합니다. 만약 std::ios_base::out which 에 설정되어 있으면, pptr() (put 영역의 다음 포인터)의 위치 재조정을 시도합니다. 만약 which 에 두 비트 모두 설정되어 있지 않으면, 연산이 실패합니다.

각 next 포인터는 다음과 같이 재배치됩니다:

  • 다음 포인터가 null이면 연산이 실패합니다.
  • 그렇지 않으면, 새로운 오프셋 newoff (타입 off_type )이 sp. offset ( ) 호출을 통해 결정됩니다. 만약 newoff 가 음수이거나, 버퍼 범위를 벗어나거나, 유효하지 않으면 연산이 실패합니다.
  • 그렇지 않으면, 다음 포인터는 gptr ( ) = eback ( ) + newoff 또는 pptr ( ) = pbase ( ) + newoff 와 같이 할당됩니다.

목차

매개변수

sp - 스트림 위치, 예를 들어 seekoff() 또는 seekpos() 로 얻은 값
which - 입력 시퀀스, 출력 시퀀스 또는 둘 다에 영향을 미치는지 정의합니다. 다음 상수들 중 하나 또는 조합일 수 있습니다:
상수 설명
in 입력 시퀀스에 영향
out 출력 시퀀스에 영향

반환값

성공 시 결과 오프셋이 pos_type 으로 변환되거나, 실패 시 pos_type ( off_type ( - 1 ) ) 가 반환됩니다.

참고 사항

seekpos() std::basic_streambuf::pubseekpos() 에 의해 호출되며, 이는 std::basic_istream::seekg() std::basic_ostream::seekp() 의 단일 인자 버전에 의해 호출됩니다.

예제

#include <cstring>
#include <iostream>
#include <strstream>
struct mybuf : std::strstreambuf
{
    mybuf(const char* str) : std::strstreambuf(str, std::strlen(str)) {}
    pos_type seekpos(pos_type sp, std::ios_base::openmode which)
    {
        std::cout << "Before seekpos(" << sp << "), size of the get area is "
                  << egptr() - eback() << " with "
                  << egptr() - gptr() << " read positions available.\n";
        pos_type rc = std::strstreambuf::seekpos(sp, which);
        std::cout << "seekpos() returns " << rc << ".\nAfter the call, "
                  << "size of the get area is "
                  << egptr() - eback() << " with "
                  << egptr() - gptr() << " read positions available.\n";
        return rc;
    }
};
int main()
{
    mybuf buf("12345");
    std::iostream stream(&buf);
    stream.seekg(2);
}

출력:

Before seekpos(2), size of the get area is 5 with 5 read positions available.
seekpos() returns 2.
After the call, size of the get area is 5 with 3 read positions available.

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 55 C++98 seekpos 가 실패 시 정의되지 않은
잘못된 스트림 위치를 반환함
pos_type ( off_type ( - 1 ) )
가 실패 시 반환됨

참고 항목

[virtual]
입력 시퀀스, 출력 시퀀스 또는 둘 모두에서 다음 포인터를 상대 주소 지정을 사용하여 재배치합니다
(가상 protected 멤버 함수)
[virtual]
입력 시퀀스, 출력 시퀀스 또는 둘 모두에서 다음 포인터를 절대 주소 지정을 사용하여 재배치합니다
( std::basic_streambuf<CharT,Traits> 의 가상 protected 멤버 함수)
[virtual]
입력 시퀀스, 출력 시퀀스 또는 둘 모두에서 다음 포인터를 절대 주소 지정을 사용하여 재배치합니다
( std::basic_stringbuf<CharT,Traits,Allocator> 의 가상 protected 멤버 함수)
[virtual]
파일 위치를 절대 주소 지정을 사용하여 재배치합니다
( std::basic_filebuf<CharT,Traits> 의 가상 protected 멤버 함수)