In the C++ programming language, seekg is a function in the fstream
library that allows you to seek to an arbitrary position in a file.
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 thedir
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.
Read more about Seekg: Example