0

Not a duplicate of C++: Wrapping vector<char> with istream

Those solutions either use boost (which is not available) or do not work with seekg on the stream. C++11 is also not available.

rdbuf()->pubsetbuf method on a stringstream is not guaranteed to work and does not on VS2015.

Given a std::vector<char>, how can I create some object compatible with std::istream which reads the data directly from the vector without copying it (as stringstream would do)?

I need to make use of the seekg function on the stream object.

My attempts so far have failed or only worked on some compilers, so I am looking for the correct answer from someone who knows all the pitfalls of C++ streams.

Community
  • 1
  • 1
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
  • See the code in [this answer](http://stackoverflow.com/a/13059195/636019). – ildjarn Sep 24 '15 at 02:16
  • @n.m. Why? This is not a homework question. I've tried several code samples from other questions but they don't work as required. – Neil Kirk Sep 24 '15 at 02:20
  • @ildjarn Doesn't work with `seekg` – Neil Kirk Sep 24 '15 at 02:24
  • 1
    Because that's how this site generally works. You show your code, other users suggest how to fix it. This is not an absolute rule, but if you already have code, why not show it? It has nothing to do with your question being or not being homework. – n. m. could be an AI Sep 24 '15 at 02:30
  • @n.m. I linked to the code I already tried. – Neil Kirk Sep 24 '15 at 02:31
  • The code needs to be in the question itself, not linked to, and results and/or error messages also shown. I have no idea what you have actually tried, but I'd hazard a guess that you need to implement `seekpos` and `seekoff` in your custom streambuf class, which linked examples don't. You can implement them in terms of `setg` and `eback, gptr, egptr`. – n. m. could be an AI Sep 24 '15 at 03:01
  • @n.m. Yes but I don't know how to do that. That is my question. Those things are hardly simple. – Neil Kirk Sep 24 '15 at 03:14
  • 1
    You should have mentioned that. Call `setg` again with the same start and end parameters, and the new current pointer parameter. Look at `boost::direct_streambuf::seekpos` for guidance. The actual implementation is in `seek_impl`. It is as straightforward as it gets. – n. m. could be an AI Sep 24 '15 at 03:33

0 Answers0