Sign in Join
Wiki copytemporary

typed files: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Typed file
22
3-A typed text file is suitable for processing files of any size. A typed text file consists of individual data records. All records in the file have the same structure. This means that each data record is of the same length.
3+A typed text file is suitable for processing files of any size. A typed text file consists of individual data records. All records in the file have the same structure. This means that each data record is of the same length.
44
55## Set the file and record structure
66
7-The structure applies to the file type and the record type. The file and data record must have the same structure.
8-
9-``` pascal
10- type
11- TMemployee = Record
12- strName : String[20];
13- sinContent : Single;
14- end;
7+The structure applies to the file type and the record type. The file and data record must have the same structure.
158```
9+type
10+ TMemployee = Record
11+ strName : String[20];
12+ sinContent : Single;
13+ end;
14+```
1615
1716## Create the file
18-
19-``` pascal
17+```
2018var
21- datFile : File of TMemployee;
22- recData : TMemployee;
23-begin
24- AssignFile(datFile, 'example.txt');
25-
26- // An existing file is deleted and re-created
27- ReWrite(datFile);
19+ datFile : File of TMemployee;
20+ recData : TMemployee;
21+ begin
22+ Assign(datFile, 'example.txt');
23+
24+ // An existing file is deleted and re-created
25+ ReWrite(datFile);
26+
27+ ...
28+ end;
29+```
2830
29- ...
30-end;
31+Or:
3132```
33+{$mode objfpc}
34+ var
35+ datFile : File of TMemployee;
36+ recData : TMemployee;
37+ begin
38+ AssignFile(datFile, 'example.txt'); // calls the above, only mode ObjFpc and mode Delphi
39+
40+ // An existing file is deleted and re-created
41+ ReWrite(datFile);
42+
43+ ...
44+ end;
45+```
3246
3347## Close the file
34-
35-``` pascal
48+```
3649begin
37- ...
38- Close(datFile); // closes the file
39- ...
40-end;
50+ ...
51+ Close(datFile); // closes the file
52+ ...
53+ end;
4154```
4255
43-## Write data record
56+Or
57+```
58+{$mode objfpc}
59+ begin
60+ ...
61+ CloseFile(datFile); // calls the above, only modes that use objpas.
62+ ...
63+ end;
64+```
4465
45-``` pascal
66+## Write data record
67+```
4668var
47- datFile : File of TMemployee;
48- recData : TMemployee;
49-begin
50- ...
51-
52- // fill the record
53- recData.strName := 'abcdefghij';
54- recData.sinContent := 1700.21;
55-
56- // write the record to the file
57- Write(datFile, recData);
58-
59- ...
60-end;
69+ datFile : File of TMemployee;
70+ recData : TMemployee;
71+ begin
72+ ...
73+
74+ // fill the record
75+ recData.strName := 'abcdefghij';
76+ recData.sinContent := 1700.21;
77+
78+ // write the record to the file
79+ Write(datFile, recData);
80+
81+ ...
82+ end;
6183```
6284
6385## Read data record
64-
65-``` pascal
86+```
6687var
67- datFile : File of TMemployee;
68- recData : TMemployee;
69-begin
70- AssignFile(datFile, 'example.txt');
71-
72- Reset(datFile); // Set the file pointer to the beginning of the file
73-
74- Read(datFile, recData); // Reads a record from the file
75- ...
76-end;
88+ datFile : File of TMemployee;
89+ recData : TMemployee;
90+ begin
91+ Assign(datFile, 'example.txt');
92+
93+ Reset(datFile); // Set the file pointer to the beginning of the file
94+
95+ Read(datFile, recData); // Reads a record from the file
96+ ...
97+ end;
7798```
7899
79100## Read file completely
80-
81-``` pascal
101+```
82102var
83- datFile : File of TMemployee;
84- recData : TMemployee;
85-begin
86- AssignFile(datFile, 'example.txt');
87- Reset (datFile);
88-
89- while not eof(datFile) // as long as data can still be read
90- do begin
91- read(datFile, recData); // read record from file
92- ...
103+ datFile : File of TMemployee;
104+ recData : TMemployee;
105+ begin
106+ Assign(datFile, 'example.txt');
107+ Reset (datFile);
108+
109+ while not eof(datFile) // as long as data can still be read
110+ do begin
111+ read(datFile, recData); // read record from file
112+ ...
113+ end;
114+
115+ Close(datFile);
93116 end;
94-
95- CloseFile (datFile);
96-end;
97117```
98118
99119## Position pointer on a data record
100-
101-``` pascal
120+```
102121begin
103- AssignFile(datFile, 'example.txt');
104- Reset(datFile);
105- ...
106-
107- Seek(datFile, 0); // position file pointer at start of first record
108- Read(datFile, recData); // read first record into recData variable
109-
110- recData.sinContent := 1602.22; // update record
122+ Assign(datFile, 'example.txt');
123+ Reset(datFile);
124+ ...
125+
126+ Seek(datFile, 0); // position file pointer at start of first record
127+ Read(datFile, recData); // read first record into recData variable
128+
129+ recData.sinContent := 1602.22; // update record
130+
131+ Seek(datFile, 0); // position file pointer at start of first record
132+ Write(datFile, recData); // write first record back
133+
134+ ...
135+ end;
136+```
111137
112- Seek(datFile, 0); // position file pointer at start of first record
113- Write(datFile, recData); // write first record back
138+## AssignFile, CloseFile vs Assign and Close
114139
115- ...
116-end;
117-```
140+Proper standard Pascal syntax, independent from compiler mode, for file handling is to use simply Assign and Close, which are defined in the system unit. AssignFile and CloseFile were introduced to mitigate clashes with other types where the syntax Assign or Close are used, like in, but not limited to, GUI apps. The latter two only work in modes that use the objpas unit since they are defined in objpas and are non-standard, although many accept them as de-facto standard. They are of great help to avoid such ambiguities, but what these functions do is simply call System.Assign and System.Close, nothing more. They are not inlined, so if you are a purist or work in non-object oriented modes, you may want to prefix Assign and Close with the system unit name: System.Assign and System.Close. That solves the same ambiguity problem but for all modes and is also slightly faster. This remark is also applicable to untyped files. It is entirely up to you what you prefer as long as you are working in one of the modes that use objpas. This simply explains the background.
118141
119142## See also
120143
121-- [[File types]]
144+ * [[File types]]
122145
123146---
124-*From the Free Pascal wiki, page "typed files", as it stood on 2020-03-02 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
147+*From the Free Pascal wiki, page "typed files", as the Internet Archive kept it on 2025-07-16, last changed on 2024-09-24. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history