In this example:
Code File:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; procedure formcreate (Sender: tobject); Procedure button1click (Sender: tobject); Procedure button2click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} uses ActiveX; Type TREC = Record Name: String [8]; age: word; end; const filename = 'C: \ temp \ test. dat '; Procedure tform1.formcreate (Sender: tobject); begin button1.caption: = 'write composite file'; button2.caption: = 'read composite file'; position: = pow.topcenter; end; procedure tform1.button1click (Sender: tobject); const mode = stgm_create or stgm_readwrite or values; var stgroot, stgsub: istorage; STM: istream; rec1: TREC; begin {create an istorage: stgroot} stgcreatedocfile (filename, mode, 0, stgroot); {create sub-istorage: stgsub} stgroot. createstorage ('stgsub', mode, 0, 0, stgsub); {create istream: STM} stgsub in sub-istorage: stgsub. createstream ('stm ', mode, 0, 0, STM); {write data} rec1.name: = 'zhang san'; rec1.age: = 99; STM. write (@ rec1, sizeof (TREC), nil); end; Procedure tform1.button2click (Sender: tobject); const mode = stgm_read or handle; var stgroot, stgsub: istorage; STM: istream; rec1: TREC; begin {exit if it is not a structured storage file} If stgisstoragefile (filename) s_ OK then exit; {Get the root istorage: stgroot} stgopenstorage (filename, nil, mode, nil, 0, stgroot); {Get sub-istorage: stgsub; note: the name of the first parameter must be consistent with that of the saved parameter} stgroot. openstorage ('stgsub', nil, mode, nil, 0, stgsub); {Get istream: STM; note: the name of the first parameter must be consistent with that when saving} stgsub. openstream ('stm ', nil, mode, 0, STM); {read data} STM. read (@ rec1, sizeof (TREC), nil); showmessagefmt ('% s, % d', [rec1.name, rec1.age]); end.
Form file:
Object form1: tform1 left = 0 Top = 0 caption = 'form1' clientheight = 107 clientwidth = 251 color = clbtnface font. charset = default_charset font. color = clwindowtext font. height =-11 font. name = 'tahoma 'font. style = [] oldcreateorder = false oncreate = formcreate pixelsperinch = 96 textheight = 13 object button1: tbutton left = 32 top = 40 width = 88 Height = 25 caption = 'button1' taborder = 0 onclick = button1click end object button2: tbutton left = 134 Top = 40 width = 88 Height = 25 caption = 'button2' taborder = 1 onclick = button2click endend