binary file: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: binary file
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## Binary file | |
| 2 | + | ||
| 3 | + | A binary file is suitable for processing files of any size. With binary files, the content of a file is read in character by character. Conversely, the file is written character by character. If a file is opened as a binary file, a character can be changed anywhere in the file. Data of the same type can now be written and read into a file defined with FILE OF as follows: | |
| 4 | + | ||
| 5 | + | ``` pascal | |
| 6 | + | var | |
| 7 | + | datFile : File of Byte; // always reads and writes a character | |
| 8 | + | datFile : File of Char; // always reads and writes a character | |
| 9 | + | datFile : File of Integer; // always reads and writes two characters | |
| 10 | + | ... | |
| 11 | + | ``` | |
| 12 | + | ||
| 13 | + | Any location in the binary file can be accessed with the SEEK command. | |
| 14 | + | ||
| 15 | + | ## Create file | |
| 16 | + | ||
| 17 | + | ``` pascal | |
| 18 | + | var | |
| 19 | + | datFile : File of Char; | |
| 20 | + | chrContent : Char; | |
| 21 | + | ||
| 22 | + | begin | |
| 23 | + | Assignfile(datFile, 'example.txt'); // Assigns the name of the file to the variable txtFile and opens the file | |
| 24 | + | ReWrite(datFile); // File will be overwritten if it exists | |
| 25 | + | chrContent := 'A'; | |
| 26 | + | Write(datFile, chrContent); // Write the first character to the new file | |
| 27 | + | ... | |
| 28 | + | end; | |
| 29 | + | ``` | |
| 30 | + | ||
| 31 | + | ## Close file | |
| 32 | + | ||
| 33 | + | ``` pascal | |
| 34 | + | var | |
| 35 | + | datFile : File of Char; | |
| 36 | + | chrContent : Char; | |
| 37 | + | ||
| 38 | + | begin | |
| 39 | + | ... | |
| 40 | + | CloseFile(datFile); // closes the file | |
| 41 | + | end; | |
| 42 | + | ``` | |
| 43 | + | ||
| 44 | + | ## Add a character to the end of an existing file | |
| 45 | + | ||
| 46 | + | For binary files, a character can be added to the end of a file using the Seek command. | |
| 47 | + | ||
| 48 | + | ``` pascal | |
| 49 | + | var | |
| 50 | + | datFile : File of char; | |
| 51 | + | chrContent : Char; | |
| 52 | + | ||
| 53 | + | begin | |
| 54 | + | AssignFile(datFile, 'example.txt'); | |
| 55 | + | Reset(datFile); // Sets the file pointer to the beginning of the file | |
| 56 | + | Seek(datFile, FileSize(datFile)); // Determines the end of the file and sets the file pointer to the end | |
| 57 | + | chrContent := 'b'; | |
| 58 | + | Write(datFile, chrContent); // Write another character at the end of the file | |
| 59 | + | ... | |
| 60 | + | ``` | |
| 61 | + | ||
| 62 | + | ## Replace a character in an existing file | |
| 63 | + | ||
| 64 | + | For binary files, you can use the Seek command to replace a character anywhere in the file. | |
| 65 | + | ||
| 66 | + | ``` pascal | |
| 67 | + | var | |
| 68 | + | datFile : File of Char; | |
| 69 | + | chrContent : Char; | |
| 70 | + | ||
| 71 | + | begin | |
| 72 | + | AssignFile(datFile, 'example.txt'); | |
| 73 | + | Reset(datFile); // Sets the file pointer to the beginning of the file | |
| 74 | + | Seek (datFile, 1); // Set the file pointer to the _second_ character in the file | |
| 75 | + | chrContent := 'f'; | |
| 76 | + | Write(datFile, chrContent); // Overwrite the second character in the file | |
| 77 | + | ... | |
| 78 | + | ``` | |
| 79 | + | ||
| 80 | + | ## Read file completely | |
| 81 | + | ||
| 82 | + | ``` pascal | |
| 83 | + | var | |
| 84 | + | datFile : File of Char; | |
| 85 | + | chrContent : Char; | |
| 86 | + | ||
| 87 | + | begin | |
| 88 | + | AssignFile(datFile, 'example.txt'); | |
| 89 | + | Reset(datFile); | |
| 90 | + | ||
| 91 | + | while not eof(datFile) // keep reading as long as there is data to read | |
| 92 | + | do begin | |
| 93 | + | read(datFile, chrContent); // reads a single character into chrContent variable | |
| 94 | + | ... | |
| 95 | + | end; | |
| 96 | + | ||
| 97 | + | CloseFile(datFile); | |
| 98 | + | end; | |
| 99 | + | ``` | |
| 100 | + | ||
| 101 | + | ## See also | |
| 102 | + | ||
| 103 | + | - [[File types]] | |
| 104 | + | ||
| 105 | + | --- | |
| 106 | + | *From the Free Pascal wiki, page "binary file", as it stood on 2022-01-28 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).* |
