In the C++ programming language, seekg() is a function in the <fstream> header of the C++ Standard Library for seeking to an arbitrary position in a file. This function is defined for std::ifstream class, for std::ofstream class there is a similar function seekp() (this is to avoid conflicts in case of classes that derive both std::istream and std::ostream, such as std::iostream).
position is the new position in the stream buffer. This parameter is an object of type std::streampos. offset is an integer value of type std::streamoff representing the offset in the stream's buffer. It is relative to the dir parameter. dir is the seeking direction. It is an object of type std::ios_base::seekdir that can take any of the following constant values:
std::ios_base::beg (offset from the beginning of the stream's buffer). std::ios_base::cur (offset from the current position in the stream's buffer). std::ios_base::end (offset from the end of the stream's buffer). Note: If the end of file on the stream has previously been reached, seekg will not reset it but will return an error in many implementations. To clear the end of the file bit first, use the clear() method. This is a relatively common mistake and if seekg() is not performing as expected, it is wise to clear the fail bit, as shown below.
Example of seekg
Example clearing the fail bit
References