std::strstreambuf::seekpos

Repositions std::basic_streambuf::gptr and/or std::basic_streambuf::pptr, if possible, to the position indicated by sp.

# Declarations

protected:
virtual pos_type seekpos( pos_type sp,
std::ios_base::openmode which =
std::ios_base::in | std::ios_base::out );

(deprecated in C++98) (removed in C++26)

# Parameters

# Return value

The resultant offset converted to pos_type on success or pos_type(off_type(-1)) on failure.

# Notes

seekpos() is called by std::basic_streambuf::pubseekpos(), which is called by the single-argument versions of std::basic_istream::seekg() and std::basic_ostream::seekp().

# Example

#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);
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 55C++98seekpos returned an undefinedinvalid stream position on failurepos_type(off_type(-1))is returned on failure

# See also