开发者

C++ equivalent of .net Stream?

开发者_JAVA百科What is the base class of all the streams in C++?

Also what is the equivalent of MemoryStream in C++?


There are several shared base classes for streams: std::ios::ios_base is the ultimate superclass, but there are also ios, istream, ostream, and iostream for different types of functionality. istream& and ostream& are what you most commonly see used as polymorphic parameter types.

Here's a pretty picture: http://www.cplusplus.com/reference/iostream/

I'm not a .Net person, so I don't know how MemoryStream is typically used, but std::stringstream probably serves at least some of the same purposes. If that doesn't do what you need, I think you're left messing about with streambufs.


If you want a pure C++ solution then Steve's answer is the right direction.

If you happen to be running on Windows and don't mind using COM then the IStream interface is a close match to System.IO.Stream. CreateStreamOnHGlobal will result in an IStream built on a memory buffer much like MemoryStream.


One base class is std::ios::ios_base, but all of the stream-related classes are templates (which caused me a lot of grief back on 2001, converting some really clever pre-standard C++ code). This allows streams to be used with char, wchar_t, and any other type somebody wants to use as a character. It's not necessarily possible to refer to a single base class in C++, since the language has multiple inheritance and the library uses it, but it looks like ios_base qualifies in this case, although it provides format functionality and not I/O.

I don't know how .NET streams work on a low-level basis, but C++ streams are fairly complicated underneath the surface.

If what you're looking for is something like MemoryStream, it looks to me like stringstream might fill the bill, or possibly the deprecated strstream (deprecated, but still in the latest draft of the C++0x standard I've seen), which IIRC allow you to attach a stream to a selected area of memory.


Looking at System.IO.Stream's documentation on MSDN, it seems to be closer to a C++ streambuf than to a C++ stream.

In the C++ IOStreams library, there are two layers:

  • the basic_streambuf is the lower layer, taking care of the actual I/O, and only dealing with sequences of "characters". There are various implementations, depending on what the I/O is made with. For example base_filebuf reads from and writes to files, basic_stringbuf reads from and writes to an in-memory string.
  • the stream (basic_istream for input, basic_ostream for output), which contains a basic_streambuf. The stream's role is formatting (i.e., transforming between values of other types and the sequence of "characters" handled by the streambuf). It does not do the actual I/O itself; it delegates it to the streambuf.

What blurs the issue is that the streams, in addition to their formatting interface, also expose more or less directly their streambuf's functionality, so a stream can be used to do non-formatted I/O. For output, for example, basic_ostream has a number of operator<< for formatted output, but it also has the "put" and "write" members allowing to (more or less) bypass the stream and directly write to the contained streambuf.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜