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?

Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • 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. – IAbstract Mar 20 '13 at 13:48

1 Answers1

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.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203