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:
I could save the current position in a separate variable and modify it manually whenever I read characters from the stream.
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With