std::strstreambuf::seekoff(3)
π std::strstreambuf::seekoff π
okay bestie, welcome to the C++ Standard Libary manual page for std::strstreambuf::seekoff (and yes, the header literally spells it βLibaryβ π). This one is all about moving stream pointers around, so grab your iced coffee and letβs get into it π§β¨
π NAME π
std::strstreambuf::seekoff β std::strstreambuf::seekoff. Thatβs the name twice on the original page, so here it is twice too, no cap π
β¨ Synopsis β¨
The synopsis is giving βprotected virtual function with a default argumentβ energy:
protected:
virtual pos_type seekoff( off_type off,
(deprecated in C++98)
ios_base::seekdir way,
(removed in C++26)
ios_base::openmode which =
ios_base::in | ios_base::out );
Heads up: this function is marked (deprecated in C++98) and (removed in C++26) π The default for which is literally ios_base::in | ios_base::out.
So what does it do? It 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. Slay π¦
- If
whichincludesios_base::inand this buffer is open for reading, then it repositions the read pointerstd::basic_streambuf::gptrinside the get area as described below. - If
whichincludesios_base::outand this buffer is open for writing, then it repositions the write pointerstd::basic_streambuf::pptrinside the put area as described below. - If
whichincludes bothios_base::inandios_base::outand the buffer is open for both reading and writing, andwayis eitherios_base::begorios_base::end, then it repositions both read and write pointers as described below. - Otherwise, this function fails. π
If the pointer (either gptr or pptr or both) is repositioned, it is done as follows:
- If the pointer to be repositioned is a null pointer and the new offset
newoffwould be non-zero, this function fails. - The new pointer offset
newoffof typeoff_typeis determined:- a) if
way == ios_base::beg, thennewoffis zero - b) if
way == ios_base::cur, thennewoffis the current position of the pointer (gptr() - eback()orpptr() - pbase()) - c) if
way == ios_base::end, thennewoffis the length of the entire initialized part of the buffer (if overallocation is used, the high watermark pointer minus the beginning pointer)
- a) if
- If
newoff + offis negative or out of bounds of the initialized part of the buffer, the function fails. - Otherwise, the pointer is assigned as if by
gptr() = eback() + newoff + offorpptr() = pbase() + newoff + off.
π Parameters π
offβ relative position to set the next pointer(s) to.wayβ defines base position to apply the relative offset to. It can be one of the following constants:begβ the beginning of a streamendβ the ending of a streamcurβ the current position of stream position indicator
whichβ defines whether the input sequences, the output sequence, or both are affected. It can be one or a combination of the following constants:inβ affect the input sequenceoutβ affect the output sequence
πΈ Return value πΈ
pos_type(newoff) on success, pos_type(off_type(-1)) on failure and if pos_type cannot represent the resulting stream position. Periodt π
π¦ Example π¦
The page says // Run this code, so hereβs the code, verbatim bestie:
#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";
}
Output:
put pos = 0 get pos = 0
put pos = 1 get pos = 1
moving both pointers from current position failed
put pos = 1 get pos = 1
put pos = 2 get pos = 1
Wrote 'a' at put position, the buffer is now: '12a'
reading at get position gives '2'
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 55, applied to C++98 β behavior as published:seekoffreturned an undefined invalid stream position on failure. Correct behavior:pos_type(off_type(-1))is returned on failure. iconic fix, honestly π
π See also π
seekpos[virtual]β repositions the next pointer in the input sequence, output sequence, or both using absolute addressing (virtual protected member function)seekoff[virtual]β repositions the next pointer in the input sequence, output sequence, or both, using relative addressing (virtual protected member function ofstd::basic_streambuf<CharT,Traits>)seekoff[virtual]β repositions the next pointer in the input sequence, output sequence, or both, using relative addressing (virtual protected member function ofstd::basic_stringbuf<CharT,Traits,Allocator>)seekoff[virtual]β repositions the file position, using relative addressing (virtual protected member function ofstd::basic_filebuf<CharT,Traits>)
okay thatβs the whole page, bestie! source: http://cppreference.com; version/date: 2024.06.10; page: std::strstreambuf::seekoff(3) β mwah ππ«Ά
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms