setf

seekg and seekp

#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( ).




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net