问题
My use case is reading a compressed file from disk, de-compressing it incrementally and using the resulting data.
Currently I am reading file contents into a temporary buffer allocated by me, point the decompression API at this buffer, and so on. The question is about understanding whether the temporary buffer really is necessary or helpful in this case.
In an experiment, I opened a stream to a text file and called get() once. In the debugger I can see that the filebuffer in the stream already contains the following characters in the text stream, as expected. (On msvc I found it under std::ifstream::_Filebuffer::_IGfirst
)
I am looking for a portable way to access this buffer I see in the debugger, feed the decompression API with it, and then continue reading the file.
I don't understand why I should copy the filebuffer to my buffer (with e.g. read()) in this particular case where I will promptly consume the buffer contents and move on. I'm not questioning the merits of buffer I/O in general.
EDIT: I did further experiments, and it seems that the internal stream buffer doesn't get used if the target of read() is itself sufficiently large. Apparently the case I was worried about, doesn't really come up.
来源:https://stackoverflow.com/questions/61321441/directly-use-c-stream-buffer-to-e-g-decompress