当前位置: 动力学知识库 > 问答 > 编程问答 >

string - How to partially read from a TStringStream, free the read data from the stream and keep the rest (the unread data)?

问题描述:

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.

网友答案:

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:

  Str.Position := Str.Position + 50;

Or like this:

  Str.Seek(50,TSeekOrigin.soCurrent);
分享给朋友:
您可能感兴趣的文章:
随机阅读: