untyped files: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: untyped files
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## Untyped text file | |
| 2 | + | ||
| 3 | + | An untyped text file is suitable for processing files of any size. With untyped files, the content of a file is read line by line. Conversely, the file is written line by line. The data records of an untyped file can be of different lengths. | |
| 4 | + | ||
| 5 | + | ## Create file | |
| 6 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | var | |
| 9 | + | datFile : TextFile; // or: datFile: text | |
| 10 | + | strName : String; | |
| 11 | + | sinContent : Single; | |
| 12 | + | begin | |
| 13 | + | Assign(datFile, 'example.txt'); // Assigns the name of the file to the variable datFile and opens the file | |
| 14 | + | ReWrite(datFile); // The old file is overwritten | |
| 15 | + | strName := 'qwert'; | |
| 16 | + | sinContent := 1800.23; | |
| 17 | + | WriteLn(datFile, strName, sinContent); // Writes the first record to the new file | |
| 18 | + | ... | |
| 19 | + | end; | |
| 20 | + | ``` | |
| 21 | + | ||
| 22 | + | In the OBJPFC or DELPHI compiler mode, the AssignFile procedure is available in addition to Assign to avoid confusion with TPersistent.Assign. | |
| 23 | + | ||
| 24 | + | ## Open file | |
| 25 | + | ||
| 26 | + | ``` pascal | |
| 27 | + | var | |
| 28 | + | datFile : TextFile; | |
| 29 | + | strName : String; | |
| 30 | + | sinContent : Single; | |
| 31 | + | begin | |
| 32 | + | Assign(datFile, 'example.txt'); // Assigns the name of the file to the datFile variable | |
| 33 | + | Reset(datFile); // Sets the file pointer to the beginning of the file | |
| 34 | + | ReadLn(datFile, strName, sinContent); // Reads the first line from the file | |
| 35 | + | ReadLn(datFile, strName, sinContent); // Reads the second line from the file | |
| 36 | + | ... | |
| 37 | + | end; | |
| 38 | + | ``` | |
| 39 | + | ||
| 40 | + | ## Close file | |
| 41 | + | ||
| 42 | + | ``` pascal | |
| 43 | + | var | |
| 44 | + | datFile : TextFile; | |
| 45 | + | strName : String; | |
| 46 | + | sinContent : Single; | |
| 47 | + | begin | |
| 48 | + | ... | |
| 49 | + | Close(datFile); // closes the file | |
| 50 | + | end; | |
| 51 | + | ``` | |
| 52 | + | ||
| 53 | + | Similar to AssignFile / Assign, the CloseFile procedure is available in OBJPFC or DELPHI compiler mode. | |
| 54 | + | ||
| 55 | + | ## Append a record to an existing file | |
| 56 | + | ||
| 57 | + | For text files (untyped files), a data record can be added to the end of the file using the Append command. | |
| 58 | + | ||
| 59 | + | ``` pascal | |
| 60 | + | var | |
| 61 | + | datFile : TextFile; | |
| 62 | + | strName : String; | |
| 63 | + | sinContent : Single; | |
| 64 | + | begin | |
| 65 | + | Assign(datFile, 'example.txt'); // Assigns the name of the file to the datFile variable | |
| 66 | + | Append(datFile); // Opens the file for write access and adds records to the end of the file | |
| 67 | + | strName := 'sdf' ; | |
| 68 | + | sinContent : = 2001.21 ; | |
| 69 | + | WriteLn(datFile, strName, sinContent); // Writes another record to the new file | |
| 70 | + | Close(datFile); // Closes the file | |
| 71 | + | ``` | |
| 72 | + | ||
| 73 | + | ## Read file completely | |
| 74 | + | ||
| 75 | + | ``` pascal | |
| 76 | + | var | |
| 77 | + | datFile : TextFile; | |
| 78 | + | strName : String; | |
| 79 | + | sinContent : Single; | |
| 80 | + | begin | |
| 81 | + | Assign(datFile, 'example.txt'); | |
| 82 | + | Reset(datFile); | |
| 83 | + | ||
| 84 | + | while not eof(datFile) // As long as data can still be read | |
| 85 | + | do begin | |
| 86 | + | read(datFile, strName, sinContent); // Read file content | |
| 87 | + | end; | |
| 88 | + | ||
| 89 | + | Close(datFile); | |
| 90 | + | end; | |
| 91 | + | ``` | |
| 92 | + | ||
| 93 | + | ## See also | |
| 94 | + | ||
| 95 | + | - [[File types]] | |
| 96 | + | ||
| 97 | + | --- | |
| 98 | + | *From the Free Pascal wiki, page "untyped files", as it stood on 2022-01-29 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).* |
