Application of Delphi Medium Flow Object

xiaoxiao2021-03-06  17

Delphi's flow object (TSTREAM derived object) has the following read and write functions:

function Read (var Buffer; Count: Longint): Longint; function Write (const Buffer; Count: Longint): Longint; procedure ReadBuffer (var Buffer; Count: Longint); procedure WriteBuffer (const Buffer; Count: Longint);

Buffer is a VAR or const type, which are all non-type parameters, so they use pointers to them.

Usually we will use the flow to handle long strings, but use string types as the buffer parameter results in errors.

Because the string includes information such as size, reference count, character storage address, etc. In order to properly process the string in the stream, it is to be converted to Pointer or PCHAR.

Examples are as follows:

var s1, s2: string; MemStream: TMemoryStream; begin MemStream: = TMemoryStream.Create; s1: = '! this is a string test'; MemStream.SetSize (length (s1)); MemStream.Write (PChar (s1) ^ , Length (S1)); SETLENGTH (S2, Length (S1)); MemStream.Position: = 0; MemStream.read (Pchar (S2) ^, Length (S1)); ShowMessage (S2); MemStream.clear; MemStream End;

转载请注明原文地址:https://www.9cbs.com/read-44745.html

New Post(0)