I have an object that writes binary data to a file. Sometimes it needs to seek to the beginning and re-write some data, to update a header of instance. I now need to avoid file i/o, so I am looking for something equivalent to a "virtual" file stream that I could seek around with.
Right now I am using a vector, but I switched from a file to improve performance as much as absolutely possible in the first place. I am also having logical problems trying to imitate fseek with a vector.
Is there a better object to use in C++? My use case is
MyVirtualStream vs; vs.Write(somebytes); vs.Write(somebytes); vs.Seek(0); vs.Read(&somestruct); somestruct.data = 555; vs.Write(struct); vs.Seek(0 + sizeof(somestruct));
std::stringstreamand dump its contents (std::stringstream::str()) to a file at once when you're done. (std::stringis well prepared to handle binary data as well as text.)