Usage of Delphi stream (3) reading files through memory stream

Source: Internet
Author: User
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) memo1: tmemo; button1: tbutton; button2: tbutton; button3: tbutton; Procedure submit (Sender: tobject ); procedure button3click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} var mstream: tmemorystream; Procedure tform1.formcreate (Sender: tobject );// Program Create a file var strlist: tstringlist; begin strlist: = tstringlist. create; strlist. add ('aaaaaaa'); strlist. add ('bbbbbbbbbb'); strlist. add ('cccccccccc'); strlist. add ('ddddddddd'); strlist. savetofile ('C: \ temp \ test.txt '); strlist. free; {memory stream created at the same time} mstream: = tmemorystream. create; end; Procedure tform1.button1click (Sender: tobject); // read the file to memobegin mstream through the stream. loadfromfile ('C: \ temp \ test.txt '); {read the file into the memory stream} memo1.lines. loadfromstream (mstream); {load the memory stream to memo1} end; Procedure tform1.button2click (Sender: tobject); // read the content in the stream using the character pointer var PC: pchar; begin PC: = mstream. memory; {point the character pointer to the memory stream} showmessage (PC [0]); {A; first character} showmessage (PC [10]); {B; the first character of the second line. There are 8 characters in each line plus 10 characters including line breaks and carriage returns.} showmessage (PC [20]); {c} showmessage (PC [30]); {d} end; Procedure tform1.button3click (Sender: tobject); // read from stream to buffer var Buffer: array [0 .. 2] of char; {Character Buffer defined} begin mstream. seek (0, sofrombeginning); mstream. read (buffer, sizeof (buffer); showmessage (buffer); {AAA} mstream. seek (10, sofrombeginning); mstream. read (buffer, sizeof (buffer); showmessage (buffer); {BBB} mstream. seek (20, sofrombeginning); mstream. read (buffer, sizeof (buffer); showmessage (buffer); {CCC} mstream. seek (30, sofrombeginning); mstream. read (buffer, sizeof (buffer); showmessage (buffer); {DDD} {about seek function: parameter 1: offset is the offset; parameter 2: origin is the reference position of the pointer, there are three optional values: sofrombeginning, sofromcurrent, sofromend sofrombeginning: start as the benchmark. At this time, the parameter offset must be greater than or equal to 0; sofromcurrent: Base on the current position; sofromend: end with the parameter offset.

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.