std::strstreambuf::seekoff

Repositions std::basic_streambuf::gptr and/or std::basic_streambuf::pptr, if possible, to the position that corresponds to exactly off characters from beginning, end, or current position of the get and/or put area of the buffer.

# Declarations

protected:
virtual pos_type seekoff( off_type off,
ios_base::seekdir way,
ios_base::openmode which = ios_base::in | ios_base::out );

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

# Parameters

# Return value

pos_type(newoff) on success, pos_type(off_type(-1)) on failure and if pos_type cannot represent the resulting stream position.

# Example

#include <iostream>
#include <strstream>
 
int main()
{
    char a[] = "123";
    std::strstream ss(a, sizeof a); // in/out
    std::cout << "put pos = " << ss.tellp()
              << " get pos = " << ss.tellg() << '\n';
 
    // absolute positioning both pointers
    ss.rdbuf()->pubseekoff(1, std::ios_base::beg); // move both forward
    std::cout << "put pos = " << ss.tellp()
              << " get pos = " << ss.tellg() << '\n';
 
    // try to move both pointers 1 forward from current position
    if (-1 == ss.rdbuf()->pubseekoff(1, std::ios_base::cur))
        std::cout << "moving both pointers from current position failed\n";
    std::cout << "put pos = " << ss.tellp()
              << " get pos = " << ss.tellg() << '\n';
 
    // move the write pointer 1 forward, but not the read pointer
    // can also be called as ss.seekp(1, std::ios_base::cur);
    ss.rdbuf()->pubseekoff(1, std::ios_base::cur, std::ios_base::out);
    std::cout << "put pos = " << ss.tellp()
              << " get pos = " << ss.tellg() << '\n';
 
    ss << 'a'; // write at put position
    std::cout << "Wrote 'a' at put position, the buffer is now: '";
    std::cout.write(a, sizeof a);
    std::cout << "'\n";
 
    char ch;
    ss >> ch;
    std::cout << "reading at get position gives '" << ch << "'\n";
}

# Defect reports

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

# See also