Well, here's an example of how to save and load. Because example, then no error handling is provided.
program textwrite; {$APPTYPE CONSOLE} type tOption=record ID: integer; Name: string[40]; end; tAuto=record Mark: string[20]; Model: string[20]; Colour: string[20]; Price: integer; Options: Array[1..3] of tOption; end; procedure WriteAuto(const t: text; const auto: tAuto); procedure WriteOption(const opt: tOption); begin WriteLn(t, opt.id, #9, opt.Name); end; var index: Integer; begin WriteLn(t, auto.Mark); WriteLn(t, auto.Model); WriteLn(t, auto.Colour); WriteLn(t, auto.Price); for index := Low(auto.Options) to High(auto.Options) do WriteOption(auto.Options[index]); end; procedure ReadAuto(const t: text; var auto: tAuto); procedure ReadOption(var opt: tOption); var Text: string; Tab: Integer; Code: Integer; begin ReadLn(t, Text); Tab := Pos(#9, text); val(copy(text, 1, Tab - 1), opt.ID, Code); opt.Name := Copy(Text, Tab + 1, Length(Text)); end; var index: Integer; begin ReadLn(t, auto.Mark); ReadLn(t, auto.Model); ReadLn(t, auto.Colour); ReadLn(t, auto.Price); for index := Low(auto.Options) to High(auto.Options) do ReadOption(auto.Options[index]); end; const Cars: array [0..1] of tAuto = ( (Mark: 'Nissan'; Model: 'Sentra'; Colour: 'Red'; Price: 100500; Options: ((Id: 1; Name: 'Pedals'), (Id: 2; Name: 'Railings'), (Id: 3; Name: ''))), (Mark: 'Renault'; Model: 'Logan'; Colour: 'Blue'; Price: 10600; Options: ((Id: 1; Name: 'Driver'), (Id: 2; Name: ''), (Id: 3; Name: '')))); var index: Integer; f: text; loaded: array [0..1] of tAuto; begin // сохранение AssignFile(f,'sort.txt'); Rewrite(f); for index := Low(Cars) to High(Cars) do WriteAuto(f, Cars[index]); Close(f); // загрузка AssignFile(f,'sort.txt'); Reset(f); for index := Low(Cars) to High(Cars) do ReadAuto(f, loaded[index]); Close(f); ReadLn; end.
The algorithm is simple:
First, in the loop for each car, we call the WriteAuto() procedure, which saves each field of the TAuto entry in a separate file line. Then, in turn, for each option calls WriteOption() , which saves the option to the same file (for a variety of IDs and Names are written in one line, separated by a tab).
For download, use ReadAuto() and ReadOption() . After reading a line from a file, ReadOption splits it into two parts by a tab character and assigns values to the corresponding tOption entry fields.
The Val () function converts a string with numbers to a number, instead you can use opt.ID := IntToStr(copy(text, 1, Tab - 1)); .
When loading from such a file, you need to know exactly how many records there are (the size of the array to load). Well, or you can write / read the number of records before reading the rest of the data.
PS: # 9 is the Tab character, it is assumed that it is not found in the name of the options.
PS: Pisano on Delphi, pure Pascal is not at hand.
WriteOption(t: text; opt: toption);andWriteAuto(t: text; auto: tauto);and call them further in the loop. - Alekcvp