| I/O manipulators | ||||
| Print functions(C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
| Synchronized Output | ||||
(C++20) | ||||
| Types | ||||
| Error category interface | ||||
(C++11) | ||||
(C++11) |
| Global objects | ||||
| Member functions | ||||
(C++11) | ||||
| Formatted input | ||||
| Unformatted input | ||||
| Positioning | ||||
basic_istream::seekg | ||||
| Miscellaneous | ||||
(C++11) | ||||
| Member classes | ||||
| Non-member functions | ||||
basic_istream& seekg( pos_type pos); | (1) | |
basic_istream& seekg( off_type off,std::ios_base::seekdir dir); | (2) | |
Sets input position indicator of the current associatedstreambuf object.
Before doing anything else,seekg clearseofbit. | (since C++11) |
seekg behaves asUnformattedInputFunction, except thatgcount() is not affected. After constructing and checking the sentry object,
Contents |
| 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:
|
*this
If an internal operation throws an exception, it is caught andbadbit is set. Ifexceptions() is set forbadbit, the exception is rethrown.
seekg(n) is not necessarily equivalent toseekg(n, ios::beg).std::basic_ifstream, for example, requires the absolute positionn to come fromtellg().
#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,
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 129 | C++98 | there was no way to indicate a failure | setsfailbit on failure |
| LWG 136 | C++98 | seekg could set the output stream | only sets the input stream |
| LWG 537 | C++98 | the type ofoff wasoff_type& | corrected tooff_type |
| returns the input position indicator (public member function)[edit] | |
| returns the output position indicator (public member function of std::basic_ostream<CharT,Traits>)[edit] | |
| sets the output position indicator (public member function of std::basic_ostream<CharT,Traits>)[edit] | |
| invokesseekpos() (public member function of std::basic_streambuf<CharT,Traits>)[edit] | |
[virtual] | repositions the file position, using absolute addressing (virtual protected member function of std::basic_filebuf<CharT,Traits>)[edit] |
[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>)[edit] |
[virtual] | repositions the next pointer in the input sequence, output sequence, or both using absolute addressing (virtual protected member function of std::strstreambuf)[edit] |
| invokesseekoff() (public member function of std::basic_streambuf<CharT,Traits>)[edit] | |
[virtual] | repositions the file position, using relative addressing (virtual protected member function of std::basic_filebuf<CharT,Traits>)[edit] |
[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>)[edit] |
[virtual] | repositions the next pointer in the input sequence, output sequence, or both, using relative addressing (virtual protected member function of std::strstreambuf)[edit] |