typed files: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | ## Typed file | |
| 2 | 2 | ||
| 3 | - | ||
| 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. | |
| 4 | 4 | ||
| 5 | 5 | ## Set the file and record structure | |
| 6 | 6 | ||
| 7 | - | ||
| 8 | - | ||
| 9 | - | ||
| 10 | - | ||
| 11 | - | ||
| 12 | - | ||
| 13 | - | ||
| 14 | - | ||
| 7 | + | The structure applies to the file type and the record type. The file and data record must have the same structure. | |
| 15 | 8 | ``` | |
| 9 | + | type | |
| 10 | + | TMemployee = Record | |
| 11 | + | strName : String[20]; | |
| 12 | + | sinContent : Single; | |
| 13 | + | end; | |
| 14 | + | ``` | |
| 16 | 15 | ||
| 17 | 16 | ## Create the file | |
| 18 | - | ||
| 19 | - | ||
| 17 | + | ``` | |
| 20 | 18 | var | |
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 24 | - | ||
| 25 | - | ||
| 26 | - | ||
| 27 | - | ||
| 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 | + | ``` | |
| 28 | 30 | ||
| 29 | - | ||
| 30 | - | ||
| 31 | + | Or: | |
| 31 | 32 | ``` | |
| 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 | + | ``` | |
| 32 | 46 | ||
| 33 | 47 | ## Close the file | |
| 34 | - | ||
| 35 | - | ||
| 48 | + | ``` | |
| 36 | 49 | begin | |
| 37 | - | ||
| 38 | - | ||
| 39 | - | ||
| 40 | - | ||
| 50 | + | ... | |
| 51 | + | Close(datFile); // closes the file | |
| 52 | + | ... | |
| 53 | + | end; | |
| 41 | 54 | ``` | |
| 42 | 55 | ||
| 43 | - | ||
| 56 | + | Or | |
| 57 | + | ``` | |
| 58 | + | {$mode objfpc} | |
| 59 | + | begin | |
| 60 | + | ... | |
| 61 | + | CloseFile(datFile); // calls the above, only modes that use objpas. | |
| 62 | + | ... | |
| 63 | + | end; | |
| 64 | + | ``` | |
| 44 | 65 | ||
| 45 | - | ||
| 66 | + | ## Write data record | |
| 67 | + | ``` | |
| 46 | 68 | var | |
| 47 | - | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - | ||
| 51 | - | ||
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 55 | - | ||
| 56 | - | ||
| 57 | - | ||
| 58 | - | ||
| 59 | - | ||
| 60 | - | ||
| 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; | |
| 61 | 83 | ``` | |
| 62 | 84 | ||
| 63 | 85 | ## Read data record | |
| 64 | - | ||
| 65 | - | ||
| 86 | + | ``` | |
| 66 | 87 | var | |
| 67 | - | ||
| 68 | - | ||
| 69 | - | ||
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 73 | - | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 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; | |
| 77 | 98 | ``` | |
| 78 | 99 | ||
| 79 | 100 | ## Read file completely | |
| 80 | - | ||
| 81 | - | ||
| 101 | + | ``` | |
| 82 | 102 | var | |
| 83 | - | ||
| 84 | - | ||
| 85 | - | ||
| 86 | - | ||
| 87 | - | ||
| 88 | - | ||
| 89 | - | ||
| 90 | - | ||
| 91 | - | ||
| 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); | |
| 93 | 116 | end; | |
| 94 | - | ||
| 95 | - | ||
| 96 | - | ||
| 97 | 117 | ``` | |
| 98 | 118 | ||
| 99 | 119 | ## Position pointer on a data record | |
| 100 | - | ||
| 101 | - | ||
| 120 | + | ``` | |
| 102 | 121 | begin | |
| 103 | - | ||
| 104 | - | ||
| 105 | - | ||
| 106 | - | ||
| 107 | - | ||
| 108 | - | ||
| 109 | - | ||
| 110 | - | ||
| 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 | + | ``` | |
| 111 | 137 | ||
| 112 | - | ||
| 113 | - | ||
| 138 | + | ## AssignFile, CloseFile vs Assign and Close | |
| 114 | 139 | ||
| 115 | - | ||
| 116 | - | ||
| 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. | |
| 118 | 141 | ||
| 119 | 142 | ## See also | |
| 120 | 143 | ||
| 121 | - | ||
| 144 | + | * [[File types]] | |
| 122 | 145 | ||
| 123 | 146 | --- | |
| 124 | - | ||
| 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).* |
