|
|
#include <iostream.h>istream &seekg(streamoff offset, ios::seek_dir origin);istream &seekg(streampos position); ostream &seekp(streamoff offset, ios::seek_dir origin); ostream &seekp(streampos 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 C++ 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 streamoff, which is defined in iostream.h. A streamoff object is capable of containing the largest valid value that offset can have.
The origin parameter is of type ios::seek_dir 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. streampos is a type defined in iostream.h 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( ).
|
|