cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : IOstream Library : ostream : seekp
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
IOstream Library
manipulators
classes:
· filebuf
· fstream
· ifstream
· ios
· iostream
· ios_base
· istream
· istringstream
· ofstream
· ostream
· ostringstream
· streambuf
· stringbuf
· stringstream
objects:
· cerr
· cin
· clog
· cout
types:
· fpos
· streamoff
· streampos
· streamsize
ostream
· ostream::ostream
· ostream::~ostream
member classes:
· ostream::sentry
member functions:
· ostream::flush
· ostream::operator<<
· ostream::put
· ostream::seekp
· ostream::tellp
· ostream::write

-

ostream::seekp public member function
ostream& seekp ( streampos pos );
ostream& seekp ( streamoff off, ios_base::seekdir dir );

Set position of put pointer

Sets the position of the put pointer.

The put pointer determines the location in the output sequence where the next output operation is going to take place.

Parameters

pos
The new position in the stream buffer. This parameter is an integral value of type streampos.
off
Integral value of type streamoff representing the offset to be applied relative to an absolute position specified in the dir parameter.
dir
Seeking direction. It is an object of type ios_base::seekdir that specifies an absolute position from where the offset parameter off is applied. It can take any of the following member constant values:
valueoffset is relative to...
ios_base::begbeginning of the stream buffer
ios_base::curcurrent position in the stream buffer
ios_base::endend of the stream buffer

Return Value

The function returns *this.

Errors are signaled by modifying the internal state flags:

flagerror
eofbit-
failbitThe parameter(s) describe a position that could not be reached.
badbitAn error other than the above happened.

Additionaly, in any of these cases, if the appropriate flag has been set with member function ios::exceptions, an exception of type ios_base::failure is thrown.

Example

// position of put pointer
#include <fstream>
using namespace std;

int main () {
  long pos;

  ofstream outfile;
  outfile.open ("test.txt");

  outfile.write ("This is an apple",16);
  pos=outfile.tellp();
  outfile.seekp (pos-7);
  outfile.write (" sam",4);

  outfile.close();

  return 0;
}

In this example, seekp is used to move the put pointer back to a position 7 characters before the end of the first output operation. The final content of the file shall be:
This is a sample

Basic template member declaration

( basic_ostream<charT,traits> )
typedef traits::pos_type pos_type;
typedef traits::off_type off_type;
basic_ostream<charT,traits> & seekp ( pos_type pos );
basic_ostream<charT,traits> & seekp ( off_type off, ios_base::seekdir dir );

See also

ostream::tellp Get position of put pointer (public member function)
istream::seekg Set position of the get pointer (public member function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us