Is TMemoryStream & TFileStream has same purpose ?
They have a similar interface, but they have different purposes. TMemoryStream
read/writes data from/to a block of memory. TFileStream
reads/write data from/to a file instead.
If we consider binary data output streaming for awhile then can we replace TMemoryStream
& TFileStream
with std::ostream
& std::ofstream
respectively?
TFileStream
writes to a file. std::ofstream
writes to a file. So, you can replace TFileStream
with std::ofstream
, yes.
TMemoryStream
is a little bit trickier. TMemoryStream
writes to a block of memory that is dynamically (re)allocated as needed. There is no standard C++ stream for writing to a block of dynamic memory. Unless you consider std::ostringstream
, which is meant for outputting strings, not binary data. Or std::vector<char>
, which is dynamic, but doesn't have a stream interface.
However, std::ostream
can work with just about any std::streambuf
you want, and there are plenty of 3rd party custom std::streambuf
implementations that can be used to read/write from/to (dynamic) memory. For example, this one writes to a std::array<char, N>
, but you can adapt it to write to a std::vector<char>
instead. Or find another implementation that suits your needs. Or write your own.
When to use compiler specific TMemoryStream
& TFileStream
over std::ostream
& std::ofstream
respectively?
Use TMemoryStream
/TFileStream
when you need to directly interface with Borland/Embarcadero's RTL/VCL/FMX frameworks. You should use standard C++ classes otherwise.