25.09.2017 Views

cpp_tutorial

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

C++<br />

File Position Pointers<br />

Both istream and ostream provide member functions for repositioning the fileposition<br />

pointer. These member functions are seekg ("seek get") for istream<br />

and seekp ("seek put") for ostream.<br />

The argument to seekg and seekp normally is a long integer. A second argument<br />

can be specified to indicate the seek direction. The seek direction can<br />

be ios::beg (the default) for positioning relative to the beginning of a<br />

stream, ios::cur for positioning relative to the current position in a stream<br />

or ios::end for positioning relative to the end of a stream.<br />

The file-position pointer is an integer value that specifies the location in the file<br />

as a number of bytes from the file's starting location. Some examples of<br />

positioning the "get" file-position pointer are:<br />

// position to the nth byte of fileObject (assumes ios::beg)<br />

fileObject.seekg( n );<br />

// position n bytes forward in fileObject<br />

fileObject.seekg( n, ios::cur );<br />

// position n bytes back from end of fileObject<br />

fileObject.seekg( n, ios::end );<br />

// position at end of fileObject<br />

fileObject.seekg( 0, ios::end );<br />

242

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!