std::basic_...buf::seekpos(3)
π std::basic_filebuf::seekpos π
okay so this page is from the C++ Standard Libary manual, and the header calls it std::basic_...buf::seekpos(3) β we're talking about std::basic_filebuf::seekpos, and it's literally the βput the file pointer where I sayβ member. bestie, it's protected and virtual, so it's giving backstage-pass energy. ποΈβ¨
π NAME π
std::basic_filebuf::seekpos - std::basic_filebuf::seekpos
β¨ Synopsis β¨
okay so the signature is protected and virtual, and yes, the default for which is the whole std::ios_base::in | std::ios_base::out combo:
protected:
virtual pos_type seekpos( pos_type sp,
std::ios_base::openmode which =
std::ios_base::in |
std::ios_base::out );
Repositions the file pointer, if possible, to the position indicated by sp. If the associated file is not open (is_open() == false), it fails immediately.
Reposition performs as follows:
- If the file is open for writing, writes the put area and any unshift sequences required by the currently imbued locale, using
overflow(). - Repositions the file pointer, as if by calling
std::fsetpos(). - If the file is open for reading, updates the get area if necessary.
β οΈ ifspwas not obtained by callingseekoff()orseekpos()on the same file, the behavior is undefined. that's the danger zone, bestie β do not freestyle this. π
π¦ Parameters π¦
spβ file position obtained byseekoff()orseekpos()called earlier on the same file.whichβ defines which of the input and/or output sequences to affect. It can be one or a combination of the following constants:inβ affect the input sequenceoutβ affect the output sequence
π Return value π
sp on success or pos_type(off_type(-1)) on failure. slay when it works, iconic failure value when it doesn't. π
πΈ 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().
Many implementations do not update the get area in seekpos(), delegating to underflow() that is called by the next sgetc().
π§ Example π§
On some implementations, the get area is emptied by seekpos() and the second underflow() is necessary to observe the effects.
// Run this code
#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);
}
Possible output:
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.
π 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:
seekposreturned an undefined invalid stream position on failure. Correct behavior:pos_type(off_type(-1))is returned on failure. - LWG 171, applied to C++98 β behavior as published: the sequence of the operations of reposition was not clear. Correct behavior: made clear.
π See also π
pubseekposβ invokesseekpos()(public member function ofstd::basic_streambuf<CharT,Traits>)seekoffβ repositions the file position, using relative addressing[virtual](virtual protected member function)fseekβ moves the file position indicator to a specific location in a file (function)
mwah π that's the whole page, bestie. source/version/date vibes: http://cppreference.com, 2024.06.10, page std::basic_...buf::seekpos(3). go touch grass now π±β¨
π¦ 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