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

Beginner
5 min read
Updated: Nov 2026
3 examples
iostream

Definition

In C++, tellp() tells you where the next write will go. For a stream that you only write to from the start, this equals the number of characters written so far.

It pairs with seekp(): save a position, write something, and come back later. 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

Call tellp on an output stream. It takes no arguments.

C++
std::streampos here = out.tellp();   // bookmark
out << "text";
out.seekp(here);                     // go back
CallWhat it does
out.tellp()Returns the current write position.
out.tellp() - beforeThe number of characters written since before was saved.
out.seekp(here)Goes back to a saved position.

Printing a number with << writes the digits too, so tellp() counts them.

Library declarations
C++
pos_type tellp();

Parameters

ParameterDescription
Nonetellp() takes no parameters.

Return Value

Return ValueDescription
std::streamposThe current write position, counted from the start.
-1Returned when the stream has failed.

C++ Examples

Example 1 — Count What You Wrote

The position grows with every write.

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

int main() {
    std::ostringstream out;

    out << "abc";
    std::cout << "after abc: " << out.tellp() << '\n';

    out << 12;
    std::cout << "after 12:  " << out.tellp() << '\n';
    return 0;
}

How It Works

  1. out << "abc" writes three characters, so the position is 3.
  2. out << 12 writes the two digits 1 and 2, so the position moves to 5.
  3. tellp() counts characters, so a number takes as many places as it has digits.

Example 2 — tellp() After seekp()

Move the write position and ask for it.

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

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

    std::cout << "at end: " << ss.tellp() << '\n';

    ss.seekp(2);
    std::cout << "after seekp(2): " << ss.tellp() << '\n';
    return 0;
}

How It Works

  1. Ten characters were written, so the write position is 10, after the last digit.
  2. seekp(2) moves the position back to 2.
  3. tellp() reports 2. The next write would replace the 2 in the text.

Example 3 — Measure How Long Some Output Is

Save the position before writing, then subtract it from the position after.

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

int main() {
    std::ostringstream out;

    std::streampos before = out.tellp();
    out << 3.5 << " units";
    std::streampos after = out.tellp();

    std::cout << "text: " << out.str() << '\n';
    std::cout << "length: " << (after - before) << '\n';
    return 0;
}

How It Works

  1. before stores the position before any text, which is 0.
  2. 3.5 takes three characters and units takes six (with the space), so the stream holds nine characters.
  3. Subtracting the two positions gives 9. This is handy for building aligned reports and file headers.

Frequently Asked Questions

The current write position of an output stream as a std::streampos, counted in characters from the start. It returns -1 on failure.
tellp is for the write (put) position of output streams. tellg is for the read (get) position of input streams.
For a stream that you write to from the start without seeking, yes. The position equals the number of characters written.
A terminal cannot seek, so the position is unknown. Use tellp() on file streams and string streams.
Save the result of tellp() in a std::streampos variable. Later call seekp(saved) to return to that place.

Next: tie()

Link an input stream to an output stream.

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

13 people found this page helpful