In the C++ programming language, seekg is a function in the iostream library (part of the standard library) that allows you to seek to an arbitrary position in a file. This function is defined for istream class - for ostream class there's a similar function Seekp (this is to avoid conflicts in case of classes that derive both istream and ostream, such as iostream).
position is the new position in the stream buffer. This parameter is an object of type streampos.
offset is an integer value of type 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 ios_base::seekdir that can take any of the following constant values:
ios_base::beg (offset from the beginning of the stream's buffer).
ios_base::cur (offset from the current position in the stream's buffer).
ios_base::end (offset from the end of the stream's buffer).
Note: If you have previously got an end of file on the stream, seekg will not reset it but will return an error in many implementations.
- use the clear() method to clear the end of file bit first. 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.