C++ /内存文件使用例程
/// c++ 内存文件使用例程
class MemoryBuffer : public std::strstreambuf { public: using Base = std::basic_streambuf<char>; using char_type = typename Base::char_type; MemoryBuffer(char* buf,size_t len) :std::strstreambuf(buf,len) { } }; class IStream :public std::istream { public: MemoryBuffer* _buffer; public: IStream(MemoryBuffer * buf) :std::istream(buf) { _buffer = buf; } ~IStream() { delete _buffer; } };
正确的用法:
std::istream* pStream = new IStream(new MemoryBuffer((char*)buf,len));