std::basic_istream::seekg(3)
πΈ NAME πΈ
okay so bestie, this is std::basic_istream::seekg(3) from the C++ Standard Libary manual β the NAME line is literally std::basic_istream::seekg β std::basic_istream::seekg, and she said what she said. π
β¨ Synopsis β¨
basic_istream& seekg( pos_type pos ); (1)
basic_istream& seekg( off_type off, std::ios_base::seekdir dir ); (2)
So this sets the input position indicator of the current associated streambuf object. No cap. π
Before doing anything else, seekg clears eofbit. (since C++11)
seekg behaves as UnformattedInputFunction, except that gcount() is not affected. After constructing and checking the sentry object:
- if
fail() != true, sets the input position indicator to absolute (relative to the beginning of the file) valuepos. Specifically, executesrdbuf()->pubseekpos(pos, std::ios_base::in)(pubseekpos, in turn, callsseekposof the specific buffer, such asbasic_filebuf::seekpos,basic_stringbuf::seekpos, orstrstreambuf::seekpos). In case of failure, callssetstate(std::ios_base::failbit). - if
fail() != true, sets the input position indicator to positionoff, relative to position, defined bydir. Specifically, executesrdbuf()->pubseekoff(off, dir, std::ios_base::in). In case of failure, callssetstate(std::ios_base::failbit).
π Parameters π
posβ absolute position to set the input position indicator tooffβ relative position (positive or negative) to set the input position indicator todirβ 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
π Return value π
*this β thatβs the whole vibe. She hands you back *this, periodt. β¨
π Exceptions π
failure if an error occurred (the error state flag is not goodbit) and exceptions() is set to throw for that state.
If an internal operation throws an exception, it is caught and badbit is set. If exceptions() is set for badbit, the exception is rethrown. π«Ά
π¦ Notes π¦
seekg(n) is not necessarily equivalent to seekg(n, ios::beg). std::basic_ifstream, for example, requires the absolute position n to come from tellg(). iykyk π
π Example π
The manual says // Run this code right here, so:
#include <iostream>
#include <sstream>
#include <string>
int main()
{
std::string str = "Hello, world";
std::istringstream in(str);
std::string word1, word2;
in >> word1;
in.seekg(0); // rewind
in >> word2;
std::cout << "word1 = " << word1 << '\n'
<< "word2 = " << word2 << '\n';
}
π Output π
word1 = Hello,
word2 = Hello,
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
- LWG 129 β applied to C++98; behavior as published: there was no way to indicate a failure; correct behavior: sets
failbiton failure. - LWG 136 β applied to C++98; behavior as published:
seekgcould set the output stream; correct behavior: only sets the input stream. - LWG 537 β applied to C++98; behavior as published: the type of
offwasoff_type&; correct behavior: corrected tooff_type.
π«Ά See also π«Ά
tellgβ returns the input position indicator (public member function)tellpβ returns the output position indicator (public member function ofstd::basic_ostream<CharT,Traits>)seekpβ sets the output position indicator (public member function ofstd::basic_ostream<CharT,Traits>)pubseekposβ invokesseekpos()(public member function ofstd::basic_streambuf<CharT,Traits>)seekpos[virtual]β repositions the file position, using absolute addressing (virtual protected member function ofstd::basic_filebuf<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 next pointer in the input sequence, output sequence, or both using absolute addressing (virtual protected member function ofstd::strstreambuf)pubseekoffβ invokesseekoff()(public member function ofstd::basic_streambuf<CharT,Traits>)seekoff[virtual]β repositions the file position, using relative addressing (virtual protected member function ofstd::basic_filebuf<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 next pointer in the input sequence, output sequence, or both, using relative addressing (virtual protected member function ofstd::strstreambuf)
This absolute icon was served from http://cppreference.com, version/date 2024.06.10, and the page is std::basic_istream::seekg(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