C++ std::ostream::seekp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, seekp() moves the put position, the place where the next write goes, in an output stream. The letter p stands for put.

With it you can go back and overwrite text that was written earlier, for example to fix a placeholder in a file. It works on file streams and string streams. Include <iostream> to use it.

This is a member function of std::ostream. You call it with a dot on a stream object, for example std::cin, std::cout, a file stream, or a std::stringstream. A stream is simply C++'s way to read or write data one piece after another.

Syntax

Give a position, or an offset together with a starting point.

C++
out.seekp(6);                     // 6 characters from the start
out.seekp(0, std::ios::end);      // to the end
out.seekp(-2, std::ios::cur);     // 2 characters back
CallWhat it does
out.seekp(6)The next write goes to position 6.
out.seekp(0, std::ios::end)The next write goes to the end.
out.seekp(-2, std::ios::cur)Moves two characters back from the current place.

Writing at a position replaces the characters that were there. It does not push the rest of the text aside.

Library declarations
C++
ostream& seekp(pos_type pos);
ostream& seekp(off_type off, ios_base::seekdir dir);

Parameters

ParameterDescription
posUsed in the first form. An absolute position from the start.
offUsed in the second form. A number of characters to move. It can be negative.
dirUsed in the second form. std::ios::beg, std::ios::cur, or std::ios::end.

Return Value

Return ValueDescription
ostream&A reference to the stream. If the move was not possible, failbit is set.

C++ Examples

Example 1 — Overwrite Part of the Text

Move to position 6 and write over the word World.

C++
#include <iostream>
#include <sstream>

int main() {
    std::stringstream ss;
    ss << "Hello World";

    ss.seekp(6);
    ss << "There";

    std::cout << ss.str() << '\n';
    return 0;
}

How It Works

  1. The stream holds Hello World. Position 6 is the W, because Hello takes positions 0 to 5.
  2. seekp(6) sends the next write to that place.
  3. There has five letters, just like World, so it replaces it completely.

Example 2 — Change the First Letter

Go back to position 0 and replace one character with put().

C++
#include <iostream>
#include <sstream>

int main() {
    std::stringstream ss;
    ss << "cat";

    ss.seekp(0);
    ss.put('b');

    std::cout << ss.str() << '\n';
    return 0;
}

How It Works

  1. After writing cat, the put position is at 3, after the last letter.
  2. seekp(0) moves it back to the first letter.
  3. put('b') writes one character at position 0, so cat becomes bat. The other letters stay.

Example 3 — Fill In a Placeholder Later

Write a line with a zero placeholder, then go back and put the real number in.

C++
#include <iostream>
#include <sstream>

int main() {
    std::stringstream ss;
    ss << "count:000\n";

    ss.seekp(6);
    ss << "042";

    std::cout << ss.str();
    return 0;
}

How It Works

  1. The first write stores count:000 and a new line. The three zeros sit at positions 6, 7, and 8.
  2. seekp(6) goes back to the first zero.
  3. Writing 042 replaces all three zeros. This trick is used to write a header first and fill in a count after the data is known.

Frequently Asked Questions

It moves the write position of an output stream, so the next write goes to the place you choose.
seekg moves the read (get) position of an input stream. seekp moves the write (put) position of an output stream. An fstream has both.
No, it only moves the position. The next write overwrites what is already there.
It may work for files, and the gap is filled by the system, but the result depends on the system. Do it only when you know what you want.
Use seekp(0, std::ios::end). For files, opening with std::ios::app is simpler and safer.

Next: setf()

Turn on one or more stream format flags.

setf() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful