| 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) |
| Public member functions | ||||
(C++11) | ||||
(C++11) | ||||
(C++26) | ||||
| Protected member functions | ||||
basic_filebuf::seekoff | ||||
| Non-member functions | ||||
(C++11) |
protected: virtual pos_type seekoff( off_type off, | ||
Repositions the file pointer, if possible, to the position that corresponds to exactlyoff characters from beginning, end, or current position of the file (depending on the value ofdir).
If the associated file is not open (is_open()==false), fails immediately.
If the multibyte character encoding is state-dependent (codecvt::encoding() returned-1) or variable-length (codecvt::encoding() returned0) and the offsetoff is not0, fails immediately: this function cannot determine the number of bytes that correspond tooff characters.
Ifdir is notstd::basic_ios::cur or the offsetoff is not0, and the most recent operation done on this filebuf object was output (that is, either the put buffer is not empty, or the most recently called function wasoverflow()), then callsstd::codecvt::unshift to determine the unshift sequence necessary, and writes that sequence to the file by callingoverflow().
Then converts the argumentdir to a valuewhence of typeint as follows:
| value ofdir | value ofwhence |
| std::basic_ios::beg | SEEK_SET |
| std::basic_ios::end | SEEK_END |
| std::basic_ios::cur | SEEK_CUR |
Then, if the character encoding is fixed-width (codecvt::encoding() returns some positive numberwidth), moves the file pointer as if bystd::fseek(file, width*off, whence).
Otherwise, moves the file pointer as if bystd::fseek(file,0, whence).
Theopenmode argument, required by the base class function signature, is usually ignored, becausestd::basic_filebuf maintains only one file position.
Contents |
| off | - | relative position to set the position indicator to | ||||||||
| dir | - | defines base position to apply the relative offset to. It can be one of the following constants:
| ||||||||
| which | - | defines which of the input and/or output sequences to affect. It can be one or a combination of the following constants:
|
A newly constructed object of typepos_type which stores the resulting file position, orpos_type(off_type(-1)) on failure.
seekoff() is called bystd::basic_streambuf::pubseekoff, which is called bystd::basic_istream::seekg,std::basic_ostream::seekp,std::basic_istream::tellg, andstd::basic_ostream::tellp.
#include <fstream>#include <iostream>#include <locale> template<typename CharT>int get_encoding(conststd::basic_istream<CharT>& stream){using Facet=std::codecvt<CharT,char,std::mbstate_t>;returnstd::use_facet<Facet>(stream.getloc()).encoding();} int main(){// prepare a 10-byte file holding 4 characters ("zß水𝄋") in UTF-8std::ofstream("text.txt")<<"\x7a\xc3\x9f\xe6\xb0\xb4\xf0\x9d\x84\x8b"; // open using a non-converting encodingstd::ifstream f1("text.txt");std::cout<<"f1's locale's encoding() returns "<< get_encoding(f1)<<'\n'<<"pubseekoff(3, beg) returns "<< f1.rdbuf()->pubseekoff(3,std::ios_base::beg)<<'\n'<<"pubseekoff(0, end) returns "<< f1.rdbuf()->pubseekoff(0,std::ios_base::end)<<'\n'; // open using UTF-8std::wifstream f2("text.txt"); f2.imbue(std::locale("en_US.UTF-8"));std::cout<<"f2's locale's encoding() returns "<< get_encoding(f2)<<'\n'<<"pubseekoff(3, beg) returns "<< f2.rdbuf()->pubseekoff(3,std::ios_base::beg)<<'\n'<<"pubseekoff(0, end) returns "<< f2.rdbuf()->pubseekoff(0,std::ios_base::end)<<'\n';}
Output:
f1's locale's encoding() returns 1pubseekoff(3, beg) returns 3pubseekoff(0, end) returns 10f2's locale's encoding() returns 0pubseekoff(3, beg) returns -1pubseekoff(0, end) returns 10
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 55 | C++98 | seekoff returned an undefinedinvalid stream position on failure | pos_type(off_type(-1)) is returned on failure |
| invokesseekoff() (public member function of std::basic_streambuf<CharT,Traits>)[edit] | |
[virtual] | repositions the file position, using absolute addressing (virtual protected member function)[edit] |
| moves the file position indicator to a specific location in a file (function)[edit] |