Is TMemoryStream & TFileStream has same purpose ?
它们具有类似的界面,但它们有不同的用途. TMemoryStream从/向内存块读取/写入数据. TFileStream代替从/向文件读/写数据.
If we consider binary data output streaming for awhile then can we replace TMemoryStream & TFileStream with std::ostream & std::ofstream respectively?
TFileStream写入文件. std :: ofstream写入文件.所以,你可以用std :: ofstream替换TFileStream,是的.
TMemoryStream有点棘手. TMemoryStream写入根据需要动态(重新)分配的内存块.没有用于写入动态内存块的标准C流.除非你考虑std :: ostringstream,它用于输出字符串,而不是二进制数据.或std :: vector< char>,它是动态的,但没有流接口.
但是,std :: ostream可以使用你想要的任何std :: streambuf,并且有很多第三方自定义std :: streambuf实现可用于从/向(动态)内存读/写.例如,this one写入std :: array< char,N>,但您可以调整它以写入std :: vector< char>代替.或者找到另一种适合您需求的实现.或者自己写.
When to use compiler specific TMemoryStream & TFileStream over std::ostream & std::ofstream respectively?
当您需要直接与Borland / Embarcadero的RTL / VCL / FMX框架连接时,请使用TMemoryStream / TFileStream.否则你应该使用标准的C类.