std::basic_istream::seekg(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

🌸 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:

  1. if fail() != true, sets the input position indicator to absolute (relative to the beginning of the file) value pos. Specifically, executes rdbuf()->pubseekpos(pos, std::ios_base::in) (pubseekpos, in turn, calls seekpos of the specific buffer, such as basic_filebuf::seekpos, basic_stringbuf::seekpos, or strstreambuf::seekpos). In case of failure, calls setstate(std::ios_base::failbit).
  2. if fail() != true, sets the input position indicator to position off, relative to position, defined by dir. Specifically, executes rdbuf()->pubseekoff(off, dir, std::ios_base::in). In case of failure, calls setstate(std::ios_base::failbit).

πŸ’– Parameters πŸ’–

  • pos β€” absolute position to set the input position indicator to
  • off β€” relative position (positive or negative) to set the input position indicator to
  • dir β€” defines base position to apply the relative offset to. It can be one of the following constants:
    • beg β€” the beginning of a stream
    • end β€” the ending of a stream
    • cur β€” 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 failbit on failure.
  • LWG 136 β€” applied to C++98; behavior as published: seekg could set the output stream; correct behavior: only sets the input stream.
  • LWG 537 β€” applied to C++98; behavior as published: the type of off was off_type&; correct behavior: corrected to off_type.

🫢 See also 🫢

  • tellg β€” returns the input position indicator (public member function)
  • tellp β€” returns the output position indicator (public member function of std::basic_ostream<CharT,Traits>)
  • seekp β€” sets the output position indicator (public member function of std::basic_ostream<CharT,Traits>)
  • pubseekpos β€” invokes seekpos() (public member function of std::basic_streambuf<CharT,Traits>)
  • seekpos [virtual] β€” repositions the file position, using absolute addressing (virtual protected member function of std::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 of std::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 of std::strstreambuf)
  • pubseekoff β€” invokes seekoff() (public member function of std::basic_streambuf<CharT,Traits>)
  • seekoff [virtual] β€” repositions the file position, using relative addressing (virtual protected member function of std::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 of std::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 of std::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