Usage of Delphi streaming (1) getting started with tmemorystream (memory stream)

Source: Internet
Author: User
Preface:

The so-called "stream" refers to a piece of data or a piece of memory;
When performing stream operations, we don't have to worry about the data in the stream. We only need to know the stream size and the current pointer position. Therefore, the stream has only two attributes:
Size, position.
Stream operation, but it is reading and writing. Therefore, the most important method of stream is read and write.
In the use of many controls, loadfromstream is used for reading, and savetostream is used for writing.

For example: (create a new project and add two memo and two buttons)

Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) memo1: tmemo; memo2: tmemo; button1: tbutton; button2: tbutton; Procedure submit (Sender: tobject ); end; var form1: tform1; implementation {$ R *. DFM} var mstream: tstream; {declare a stream object} procedure tform1.formcreate (Sender: tobject); begin mstream: = tmemorystream. create; {tstream is an abstract class and can only be instantiated by its subclass. Here we use a memory stream to generate an instance} memo1.lines. text: = 'abcdefghijklmnopqrstuvwxy'; {give memo1 an initial value} end; Procedure tform1.button1click (Sender: tobject); begin memo1.lines. savetostream (mstream); {write the content in memo1 to the stream} showmessage (inttostr (mstream. size); {26, current stream size} showmessage (inttostr (mstream. position); {26, current stream pointer} end; Procedure tform1.button2click (Sender: tobject); begin mstream. position: = 4; {adjust the current pointer position of the stream} memo2.lines. loadfromstream (mstream); {read the content in the stream to memo2} {the content in memo2 should be: efghijklmnopqrstuvwxyz. If the position is 0, the content read by memo2 will be: whether the position is equal to the size, if it is 26, memo2 cannot read anything .} end; Procedure tform1.formdestroy (Sender: tobject); begin mstream. free; {when the stream is released, the memory used will also be released} end; end.
 
   
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.