如何从 TStringStream 中部分读取,从流中释放读取的数据并保留其余部分(未读取的数据)?
我想要做什么:假设我有一个 TStringStream,它只读取一个包含 100 个字符的字符串。如果我调用 .ReadString(50),我将获得该流的前 50 个字符,并且其光标将放置在位置 51 上。
我的问题是:如何将该流中的字符 1 到 50 扔到一个快速而干净的方法?我想稍后再读剩下的(51 到 100)。
提前致谢。
What I want to do: lets suppose I have a TStringStream that just read a string with 100 characters. If I call .ReadString(50), I will get the first 50 characters of this stream and its cursor is going to be placed on the position 51.
My question is: how do I toss the characters 1 to 50 in this stream in a fast and clean way? I want to read the rest (51 to 100) later.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你无法做你希望做的事。字符串流的数据是一个 Delphi 字符串,它存储为单个内存块。内存块是原子的,它们不能被分割。您无法释放内存块的某些部分。
如果您确实需要将内存返回给内存管理器,那么您应该创建一个新字符串,并删除已处理的数据。然后,您可以使用此新输入重新创建字符串流并销毁以前的字符串流。
话虽如此,除了增加内存碎片之外,很难看出这样做还有什么其他作用。如果涉及的内存大小足够大,并且字符串流持续足够长的时间,那么这可能是一种明智的方法。否则,这听起来像是一种实际上会阻碍性能的优化尝试。
也许除字符串流之外的其他类可能更合适,但在不了解更多细节的情况下很难给出建议。
You cannot do what you are hoping to do. The string stream's data is a Delphi string which is stored as a single memory block. Memory blocks are atomic, they cannot be split. You cannot free some part of a memory block.
If you really need to return memory to the memory manager then you should create a new string with the already processed data removed. You can then re-create your string stream with this new input and destroy the previous string stream.
Having said that, it's hard to see that doing much other than increasing your memory fragmentation. If the sizes of memory involved are large enough, and if the string stream persists for long enough, then this just might be a sensible approach. Otherwise it sounds like an attempt to optimise that actually would hinder performance.
Perhaps some class other than string stream could be more appropriate but it's very hard to advise without knowing more details.
你不能这样做。如果您确实需要这样做,您应该编写自己的类来实现流接口,并且它可以让您一次处理一些数据并释放您想要释放的任何数据。请注意,您只能浏览一次数据,因为您现在已删除数据。也就是说,再次寻找起点将变得不可能,并且您当前的流“位置”将是一个谎言。
简而言之,听起来你很困惑。
You can't do this. If you really need to do this, you should write your own class that implements the stream-interface and which would let you process some data a little bit at a time and free whatever you want to free. Note that you would only be able to go through the data once, since you've now deleted your data. That is, seeking to the beginning again would become impossible, and your current stream "position" would be a lie.
In short, sounds like you're confused.
如果我理解正确的话,您应该在流中向前跳过哪一个?
你可以这样做:
或者像这样:
If I understand correctly you which to skip forward in the stream?
You can do:
Or like this: