Raven
Raven

Reputation: 345

tellg and seekg not working when wrap vector<char> in istream

As this link [https://stackoverflow.com/a/8815308/1068715] said:

I use setg(vec.data(), vec.data(), vec.data() + vec.size()); to initialize a streambuf. After that, read() on istream working properly, but tellg() always return -1, and seekg() always failed.

anyone can help?

Upvotes: 0

Views: 222

Answers (2)

ZeroDefect
ZeroDefect

Reputation: 795

Thanks for asking this; I came across the same problem after referencing the same link.

I'd just like to add that if you're having a seekg() problem and concerned about implementing this, I would vouch for the Boost option:

boost::iostreams::basic_array_source<char> isrc(vec.data(), vec.size());
boost::iostreams::stream< typeof(isrc) > istr(isrc);

because you get this seek functionality for free straight out the box instead of having to write an implementation yourself.

Upvotes: 1

T.C.
T.C.

Reputation: 137425

seekg and tellg ask the streambuf via pubseekoff and pubseekpos to actually perform the work. The default implementation of these functions simply fail.

You need to implement seekoff and seekpos in your streambuf.

Upvotes: 1

Related Questions