embarcadero/borland TMemoryStream和TFileStream的标准C++等价物是什么?

What are the standard C++ equivalents for embarcadero/borland TMemoryStream and TFileStream?

本文关键字:C++ 标准 等价物 是什么 TFileStream borland TMemoryStream embarcadero      更新时间:2023-10-16

我找到了遗留源代码,在这里摘录 -

TMemoryStream *DFile = new TMemoryStream;
TFileStream*BFile = new TFileStream;

以下是上述类别的一些官方记录数据:

  • TMemoryStream Wiki & TMemoryStream Doc with example
  • TFileStream Wiki & TFileStream Doc(同一件事)

  • TMemoryStreamTFileStream有相同的目的吗?

  • 如果我们考虑一段时间的二进制数据输出流,那么我们可以 将TMemoryStream&TFileStream替换为std::ostream& 分别std::ofstream

  • (我有点困惑)何时分别使用编译器特定的TMemoryStreamTFileStream而不是std::ostreamstd::ofstream

    • 如果我们进行上述事情,我们可以获得什么好处?

TMemoryStream 和 TFileStream 有相同的目的吗?

它们具有相似的界面,但用途不同。TMemoryStream从内存块读取/写入数据。TFileStream改为从文件读取/写入数据。

如果我们考虑二进制数据输出流一段时间,那么我们可以分别用std::ostreamstd::ofstream替换TMemoryStreamTFileStream吗?

TFileStream写入文件。std::ofstream写入文件。 所以,你可以用TFileStream替换std::ofstream,是的。

TMemoryStream有点棘手。TMemoryStream写入根据需要动态(重新)分配的内存块。 没有用于写入动态内存块的标准C++流。 除非您考虑std::ostringstream,否则它用于输出字符串,而不是二进制数据。 或者std::vector<char>,它是动态的,但没有流接口。

但是,std::ostream几乎可以使用您想要的任何std::streambuf,并且有很多第三方自定义std::streambuf实现可用于从(动态)内存读取/写入。例如,这个写入std::array<char, N>,但您可以调整它以写入std::vector<char>。或者找到适合您需求的其他实现。或者写你自己的。

何时分别使用编译器特定的TMemoryStreamTFileStream而不是std::ostreamstd::ofstream

当您需要直接与Borland/Embarcadero的RTL/VCL/FMX框架接口时,请使用TMemoryStream/TFileStream。 否则,应使用标准C++类。

流是 IO 库C++的一部分。特别是,文件流由std::fstream(http://en.cppreference.com/w/cpp/io/basic_fstream) 支持,内存中流由std::stringstream(http://en.cppreference.com/w/cpp/io/basic_stringstream) 表示