typed files: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: typed files
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## Typed file | |
| 2 | + | ||
| 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 | + | ||
| 5 | + | ## Set the file and record structure | |
| 6 | + | ||
| 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; | |
| 15 | + | ``` | |
| 16 | + | ||
| 17 | + | ## Create the file | |
| 18 | + | ||
| 19 | + | ``` pascal | |
| 20 | + | var | |
| 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); | |
| 28 | + | ||
| 29 | + | ... | |
| 30 | + | end; | |
| 31 | + | ``` | |
| 32 | + | ||
| 33 | + | ## Close the file | |
| 34 | + | ||
| 35 | + | ``` pascal | |
| 36 | + | begin | |
| 37 | + | ... | |
| 38 | + | Close(datFile); // closes the file | |
| 39 | + | ... | |
| 40 | + | end; | |
| 41 | + | ``` | |
| 42 | + | ||
| 43 | + | ## Write data record | |
| 44 | + | ||
| 45 | + | ``` pascal | |
| 46 | + | var | |
| 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; | |
| 61 | + | ``` | |
| 62 | + | ||
| 63 | + | ## Read data record | |
| 64 | + | ||
| 65 | + | ``` pascal | |
| 66 | + | var | |
| 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; | |
| 77 | + | ``` | |
| 78 | + | ||
| 79 | + | ## Read file completely | |
| 80 | + | ||
| 81 | + | ``` pascal | |
| 82 | + | var | |
| 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 | + | ... | |
| 93 | + | end; | |
| 94 | + | ||
| 95 | + | CloseFile (datFile); | |
| 96 | + | end; | |
| 97 | + | ``` | |
| 98 | + | ||
| 99 | + | ## Position pointer on a data record | |
| 100 | + | ||
| 101 | + | ``` pascal | |
| 102 | + | begin | |
| 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 | |
| 111 | + | ||
| 112 | + | Seek(datFile, 0); // position file pointer at start of first record | |
| 113 | + | Write(datFile, recData); // write first record back | |
| 114 | + | ||
| 115 | + | ... | |
| 116 | + | end; | |
| 117 | + | ``` | |
| 118 | + | ||
| 119 | + | ## See also | |
| 120 | + | ||
| 121 | + | - [[File types]] | |
| 122 | + | ||
| 123 | + | --- | |
| 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).* |
