- Seekg
In the C++ programming language, seekg is a function in the
library that allows you to seek to an arbitrary position in a file.fstream *
position
is the new position in the stream buffer. This parameter is an object of type streampos.
*offset
is and integer value of type streamoff representing the offset in the stream's buffer. It is relative todir
parameter.dir
is the seeking direction. It is an object of typeios_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 theclear()
method to clear the end of file bit first. This is a relatively common mistake and ifseekg()
is not performing as expected, it is wise to clear the fail bit, as shown below.Example
Example
Wikimedia Foundation. 2010.