1

This answer shows how to write to an array with stringstream, but can we obtain the total number of characters written? Surely the stringstream has some information to know where to put the next character, but I don't know how to access it.

The OP of the linked question even asks this in a comment but I wanted a separate question to be sure.

I know ostrstream has a pcount method, unfortunately, it is deprecated.

2
  • 2
    tellp()? Commented Aug 29, 2022 at 21:54
  • 1
    “unfortunately, it is deprecated” — deprecated doesn’t mean it doesn’t work. It means only that it might be removed from the standard in the future. If it gets removed, sane implementations will continue to provide it. Commented Aug 29, 2022 at 22:00

1 Answer 1

3

The std::ostrstream class itself is deprecated.

Use std::ostringstream instead, which has a tellp() method.

#include <sstream> int main() { char buf[1024]; std::ostringstream stream; stream.rdbuf()->pubsetbuf(buf, sizeof(buf)); auto start = stream.tellp(); stream << "Hello " << "World " << std::endl; auto written = stream.tellp() - start; //... } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.