|
|
#include <iostream>istream &seekg(off_type offset, ios::seekdir origin)istream &seekg(pos_type position); ostream &seekp(off_type offset, ios::seekdir origin); ostream &seekp(pos_type position);
The seekg( ) function is a member of istream, and the seekp( ) function is a member of ostream.
In C++’s I/O system, you perform random access using the seekg( ) and seekp( ) functions. To this end, the I/O system manages two pointers associated with a file. One is the get pointer, which specifies where in the file the next input operation will occur. The other is the put pointer, which specifies where in the file the next output operation will occur. Each time an input or an output operation takes place, the appropriate pointer is automatically sequentially advanced. However, using the seekg( ) and seekp( ) functions, it is possible to access the file in a nonsequential fashion.
The two-parameter version of seekg( ) moves the get pointer offset number of bytes from the location specified by origin. The two-parameter version
of seekp( ) moves the put pointer offset number of bytes from the location specified by origin. The offset parameter is of type off_type, which is capable of containing the largest valid value that offset can have.
The origin parameter is of type seekdir and is an enumeration that has these values:
ios::beg | Seek from beginning |
ios::cur | Seek from current position |
ios::end | Seek from end |
The single-parameter versions of seekg( ) and seekp( ) move the file pointers to the location specified by position. This value must have been previously obtained using a call to either tellg( ) or tellp( ), respectively. pos_type is a type that is capable of containing the largest valid value that position can have. These functions return a reference to the associated stream.
Related functions are tellg( ) and tellp( ).
|
|