1

Does the std::ostream store the bytes that has been sent to the output internally?
If it does, is there any way to read them back?

Let's say, I need to check what byte has been sent to the output stream 9 bytes ago in order to send it to the output again at current position. Is there any way to do it without maintaining a separate buffer with a copy of all the output so far? Could std::streambuf be used for that?

If there isn't any way to get the grip on the data that has been sent to the output, what is the best way to do it? (Copy a fragment of the data that has been already sent to the output and output it again at the current position.)

Edit: Perhaps I'll give you some background information:
I have a piece of code written which uses two pointers into buffers: source pointer and destination pointer. It can copy some data from source to destination in a stream-like fashion, moving the pointers (source and destination) as it copies the data.
But sometimes it also needs to copy the data inside the destination buffer: from one place in the destination buffer into another place in the destination buffer. Let's say it reads from 9 bytes ago and writes at the current position, moving both pointers after the copying.

Now I'm trying to rewrite this code from low-level C to C++ using input/output iterators. At first I used std::istreambuf_iterator in place of the source pointer, tied to either the std::cin or std::ifstream, and std::ostreambuf_iterator in place of the destination pointer. At first it seemed to work, until I realized that I need also the possibility to read back from the destination buffer :P But the std::ostreambuf_iterator can only output and move further. It cannot read back nor move back :/ So I need something else, but I'm not sure what exactly. What I know for sure is that I will never need to read further than 65535 bytes back from the current position, so I guess that this is the size of the output buffer I need to maintain myself. But what type of iterators should I use for my function which used the source and destination pointers previously? std::istreambuf_iterator and std::ostreambuf_iterator doesn't seem to be well-suited for that. I need to be able to construct a third iterator from the current position in the destination buffer, but with some negative offset, and use it for reading inside my function.

BarbaraKwarc
  • 2,667
  • 2
  • 14
  • 17
  • 3
    At any given time, there's a good chance you have a fair amount of recent data stored in the streambuf--but when the streambuf gets full, it gets flushed, which writes it all out to the external file, and then empties the streambuf. You could write a streambuf that (for example) used a pair of buffers, so it always had the N most-recently written bytes (assuming at least N bytes had been written). – Jerry Coffin Aug 04 '16 at 00:31
  • Hmm... so even if it does store the data internally, there's no way to control how much data there is and how long they are there, so a better idea would be to buffer it explicitly myself? – BarbaraKwarc Aug 04 '16 at 00:34
  • Yes, I think you pretty much need to. – Jerry Coffin Aug 04 '16 at 00:36
  • I added some background information of what exactly I'm trying to do. – BarbaraKwarc Aug 04 '16 at 00:52

0 Answers0