std::strstreambuf::seekpos(3)
π NAME π
okay bestie, welcome to std::strstreambuf::seekpos(3) from the C++ Standard Libary manual. This is the std::strstreambuf::seekpos page β yes, the NAME line is literally std::strstreambuf::seekpos - std::strstreambuf::seekpos β and it's giving protected-virtual main character energy. π¦π
β¨ Synopsis β¨
so here's the exact signature, served with all its original little vintage annotations:
protected:
virtual pos_type seekpos( pos_type sp,
(deprecated in C++98)
std::ios_base::openmode which =
(removed in C++26)
std::ios_base::in |
std::ios_base::out );
heads up:(deprecated in C++98)and(removed in C++26)β yes, this one is vintage and endangered, so enjoy the vibe while it's here. π
seekpos() repositions std::basic_streambuf::gptr and/or std::basic_streambuf::pptr, if possible, to the position indicated by sp.
If std::ios_base::in is set in which, it attempts to reposition gptr() β the next pointer in the get area. If std::ios_base::out is set in which, it attempts to reposition pptr() β the next pointer in the put area. If neither bit is set in which, the operation fails. no cap.
Each next pointer gets repositioned like this:
- If the next pointer is null, the operation fails.
- Otherwise, the new offset
newoff(of typeoff_type) is determined by callingsp.offset(). Ifnewoffis negative, out of bounds of the buffer, or invalid, the operation fails. - Otherwise, the next pointer is assigned as if by
gptr() = eback() + newofforpptr() = pbase() + newoff.
π«Ά Parameters π«Ά
spβ stream position, such as one obtained byseekoff()orseekpos().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 π
The resultant offset converted to pos_type on success, or pos_type(off_type(-1)) on failure. periodt. β¨
π 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(). iykyk. π«Ά
π¬ Example π¬
okay this demo is iconic, bestie:
// Run this code
#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);
}
πΊ Output πΊ
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.
π©Ή Defect reports π©Ή
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DRLWG 55; Applied toC++98; Behavior as publishedseekposreturned an undefined invalid stream position; Correct behaviorpos_type(off_type(-1))is returned on failure.
π See also π
seekoff[virtual]β repositions the next pointer in the input sequence, output sequence, or both, using relative addressing (virtual protected member function).seekpos[virtual]β repositions the next pointer in the input sequence, output sequence, or both using absolute addressing (virtual protected member function ofstd::basic_streambuf<CharT,Traits>).seekpos[virtual]β repositions the next pointer in the input sequence, output sequence, or both using absolute addressing (virtual protected member function ofstd::basic_stringbuf<CharT,Traits,Allocator>).seekpos[virtual]β repositions the file position, using absolute addressing (virtual protected member function ofstd::basic_filebuf<CharT,Traits>).
made with π from http://cppreference.com, version/date 2024.06.10, for std::strstreambuf::seekpos(3) β bye bestie! π«Άπ
π¦ 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