streambuf get streampos
Asked Answered
P

1

5

I use the C++ streambuf class for a compiler project and need a convenient way to get the current position in the stream.

There are two member functions, streambuf::pubseekpos and a streambuf::pubseekoff, to modify the position and I am quite confused about the absence of a streambuf::pubgetpos member function (or something similar) to read it.

There seem to be two possible workarounds:

  1. I could save the current position in a separate variable and modify it manually whenever I read characters from the stream.

  2. I could call streambuf::pubseekoff(0, ios_base::cur), which returns the new stream position.

The second option seems usable but inefficient and unaesthetic for such a trivial task. Is there a better way to do it?

Pork answered 20/3, 2013 at 13:32 Comment(1)
Option 2, inefficient or not, appears to be a fine option. I wouldn't be concerned about aesthetics when it comes to C++. :) Maybe the API for streambuf is a minimal API.Kowalczyk
P
7

The streambuf doesn't have a separate interface for reading the position. However, istream and ostream do (tellg and tellp respectively).

Interestingly, the streams use your option 2 to get their positions, so it is just fine.

Pyretotherapy answered 20/3, 2013 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.