简体   繁体   中英

unformatted i/o to and from memory

I'm looking for a way to write unformatted data to memory using the C++ standard library. I'd like a class which inherits istream/ostream and works like ifstream/ofstream, but backed by memory instead of a file on disk.

This way I can work with istreams and ostreams and use operator<< and operator>> to read/write binary data, and I don't need to know whether the data is streaming into memory or onto disk.

I thought perhaps istringstream/ostringstream could be configured to write unformatted output via operator<< and operator>>, but I couldn't see a simple way to do this.

So I wasn't able to find anything like this in the C++ standard library or in boost, but it seems like the sort of thing that should be there. Any advice would be most appreciated!

You can't use the << and >> operators to write/read unformatted data (they are dealing only with textual input and output), but you can use the write method of output streams and the read method of input streams.

And as std::string actually don't really care about the data it holds you should be able to use stringstream (and its output/input variants).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM