Good afternoon, there was a need to work with a certain block of memory through the istream interface, without copying this data. I read the help on the streams here: , but I still have not figured out what to do.

I suppose you need to create an istringstream class object and associate a buffer with a specific block of memory? If this is true, then how to do it? The buffer is a closed member, and I saw only the rdbuf () method for reading the buffer address, how to determine the pointer to the buffer, I did not understand ... In short, I have no idea what to do, I will be grateful for the help and hints .

Here is what I did, “to make it work right now”:

HRSRC hRes=FindResource(0,L"coordsx",RT_RCDATA); HGLOBAL hGlob=LoadResource(0,hRes); unsigned int sz = SizeofResource(0,hRes); std::string ts((char*)hGlob,sz); std::istringstream ins(ts,std::ios_base::in|std::ios_base::binary); 

ins is here a target object, it has an istream interface, which is what is required in my situation, but as far as I understand, in this process there is already twice as much data copying - a simple area of ​​memory => std :: string => std :: stringbuf , here I want to somehow manage to do without unnecessary copying ...

    2 answers 2

    If you do not write your wrapper, then the solution from boost :: iostreams

    At one time, when I implemented memory-based serialization, I used the following type of code:

     // See the following sample powered by 'boost::iostreams' library: // --------------------------------------------------------------- // Create a stream based on 'std::vector' back inserter. typedef boost::iostreams::stream< boost::iostreams::back_insert_device<std::vector<char> > > Stream; serialize(object, Stream(std::vector<char>()), GzipCompressor()); // The same technique could be applied for deserialization from // array-like containers: // ------------------------------------------------------------ // Create a stream that iterates over a sequence and use it in // the object loading wrapper. typedef boost::iostreams::stream< boost::iostreams::array_source> Stream; deserialize(object, Stream(&*source.begin(), source.size()), GzipCompressor()); 
    • one
      Wow, it means that intuition did not let me down about boost)) - skegg

    It seems to me that if you really want difficulties, you need to write your own stream buffer and then associate it with a file stream. For writing the buffer, the main thing is to observe the interface. He is well described by Josattis in “C ++. Standard Library,” ch. 13.

    You can also delve into Boost.Iostreams. Maybe there is something suitable there.

    • The nature of the problem is that the data was transferred from an external file to resources. I see two ways, the first is to load the resource into memory and do something like what I described in the question, the second is to get the offset of the resource data in the file and work with them through ifstream by opening the executable directly with it ... might be better to use this second method? - Anton Dmitriev
    • Well, you know better. Try, experiment. I can’t see all the nuances of this problem from afar. - skegg
    • In any case, you need to load the data into memory. - fogbit
    • Of course, I myself did not doubt this for a second - Anton Dmitriev