Namespaces
Variants

std::basic_filebuf<CharT,Traits>:: 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 ) ;

가능한 경우 파일 포인터를 sp 가 가리키는 위치로 재배치합니다. 연결된 파일이 열려 있지 않은 경우( is_open ( ) == false ) 즉시 실패합니다.

Reposition은 다음과 같이 수행됩니다:

1) 파일이 쓰기용으로 열려 있는 경우, 현재 임뷰드된 로캘에 의해 요구되는 퍼트 영역과 언시프트 시퀀스를 overflow() 를 사용하여 기록합니다.
2) 파일 포인터를 재위치합니다, 마치 std::fsetpos() 를 호출하는 것처럼.
3) 파일이 읽기용으로 열려 있는 경우, 필요한 경우 get 영역을 업데이트합니다.

만약 sp 가 동일한 파일에서 seekoff() 또는 seekpos() 를 호출하여 얻어진 것이 아니라면, 동작은 정의되지 않습니다.

목차

매개변수

sp - 동일한 파일에서 이전에 호출된 seekoff() 또는 seekpos() 로 얻은 파일 위치
which - 입력 및/또는 출력 시퀀스 중 어느 것을 영향을 줄지 정의합니다. 다음 상수들 중 하나 또는 조합일 수 있습니다:
상수 설명
in 입력 시퀀스에 영향
out 출력 시퀀스에 영향

반환값

sp 성공 시 또는 pos_type ( off_type ( - 1 ) ) 실패 시 반환됩니다.

참고 사항

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

많은 구현에서는 seekpos() 에서 get 영역을 업데이트하지 않고, 다음 underflow() 가 호출될 때까지 미루며, 이는 다음 sgetc() 에 의해 호출됩니다.

예제

일부 구현에서는 get 영역이 seekpos() 에 의해 비워지며, 효과를 관찰하기 위해서는 두 번째 underflow() 호출이 필요합니다.

#include <fstream>
#include <iostream>
struct mybuf : std::filebuf
{
    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::filebuf::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";
// uncomment if get area is emptied by seekpos()
//        std::filebuf::underflow();
//        std::cout << "after forced underflow(), size of the get area is "
//                  << egptr() - eback() << " with "
//                  << egptr() - gptr() << " read positions available.\n";
        return rc;
    }
};
int main()
{
    mybuf buf;
    buf.open("test.txt", std::ios_base::in);
    std::istream stream(&buf);
    stream.get(); // read one char to force underflow()
    stream.seekg(2);
}

가능한 출력:

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

결함 보고서

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

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

참고 항목

seekpos ( ) 를 호출합니다
( std::basic_streambuf<CharT,Traits> 의 public 멤버 함수)
[virtual]
상대 주소 지정을 사용하여 파일 위치를 재배치합니다
(virtual protected 멤버 함수)
파일 위치 표시자를 파일의 특정 위치로 이동합니다
(함수)