Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

streambuf get streampos

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?

like image 787
user2190941 Avatar asked Mar 20 '13 13:03

user2190941


1 Answers

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.

like image 167
Bo Persson Avatar answered Sep 25 '22 17:09

Bo Persson