There are tAuto entries, tOption and an array from tAuto

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; tCars= array of tAuto; 

You need to write two procedures:

1) write the code for reading the elements of this array into a text file of the root of the program folder

2) the second procedure should be responsible for outputting the saved data from the file to an empty console array

My attempts to realize my plans:

 Procedure SaveInFile(var Cars: tCars); Var f: text; i: integer; Begin AssignFile(f,'sort.txt'); Rewrite(f); for i:= low(cars) to high(cars) do write(f, cars[i]); close(f); End; Procedure OpenFile(var Cars: tCars); Var f: file of tAuto; i,l: integer; Begin AssignFile(f,'sort.txt'); Reset(f); l := FileSize(f); SetLength(Cars,l); for i:= 0 to l-1 do read(f, cars[i]); close(f); End; 

The result: the compiler does not produce any errors, does not save the file and displays nothing. Please indicate what needs to be corrected / rewritten in order for the code to perform its function.

  • This does not work that way, you will have to disassemble each entry into the fields with pens and write it into a test file. And also read. Make a procedure of type WriteOption(t: text; opt: toption); and WriteAuto(t: text; auto: tauto); and call them further in the loop. - Alekcvp
  • Could you demonstrate how this is recorded, please? - A.Hall

1 answer 1

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.

  • Could you explain how your program works? In Pascal ABC, it does not compile, it gives an error: Program1.pas (44): Runtime Error: The index and length should indicate the position in the line. Parameter name: length On line: opt.Name: = Copy (Text, Tab + 1, MaxInt); - A.Hall
  • Copy(Text, Tab + 1, MaxInt) - copies the contents of the line from the Tab + 1 position to the end of the line. Try instead of MaxInt to write Length(Text) [corrected answer]. - Alekcvp
  • Now the code is compiled, but does not output anything and does not save the file - A.Hall
  • If you don’t even have a file, try specifying the file name with the full path: not 'sort.txt' , but, for example, 'c:\sort.txt' and look for it there. - Alekcvp
  • I tried both this and that. The file with the program lies in a separate folder. When saving and compiling a text file is not created anywhere - A.Hall