File: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: File
| Old | New | Line | |
|---|---|---|---|
| 1 | + | In a [[Pascal]] program a variable of type **file** may be used to read, write or both read and write to a file. A file variable is (usually) associated with a file on a disk using the [[AssignFile]] procedure (older, equivalent name: just [[Assign]]). (See [[File Handling In Pascal]] for a more object oriented approach and more examples.) | |
| 2 | + | ||
| 3 | + | # File types | |
| 4 | + | ||
| 5 | + | ## Untyped binary | |
| 6 | + | ||
| 7 | + | The type **file** represents an *untyped* binary file. *Untyped* means it's just a sequence of data (bytes). It may be used to store essentially anything. It can be read by **BlockRead** and written to by **BlockWrite** procedures. The word *file* itself is a [[Reserved word|reserved word]] in Pascal. Example: | |
| 8 | + | ||
| 9 | + | ``` pascal | |
| 10 | + | {$mode objfpc}{$H+} | |
| 11 | + | var | |
| 12 | + | MyFile: file; | |
| 13 | + | Data: array [0..99] of Byte; | |
| 14 | + | begin | |
| 15 | + | AssignFile(MyFile, 'a'); | |
| 16 | + | Reset(MyFile, 1 { size of read chunk }); | |
| 17 | + | try | |
| 18 | + | BlockRead(MyFile, Data, SizeOf(Data)); | |
| 19 | + | finally | |
| 20 | + | CloseFile(MyFile); | |
| 21 | + | end; | |
| 22 | + | end. | |
| 23 | + | ``` | |
| 24 | + | ||
| 25 | + | For larger example code, see [BlockRead documentation](http://www.freepascal.org/docs-html/rtl/system/blockread.html). | |
| 26 | + | ||
| 27 | + | ## Typed binary | |
| 28 | + | ||
| 29 | + | The type [**file of \<type\>**](http://www.freepascal.org/docs-html/ref/refsu21.html), where \<type\> is any simple type (no references/pointers) or record type, is a binary file representing a sequence of \<type\> values. For example, you can have files that are a sequence of integers, floating-point values, or records (as long as all record fields are also simple types). | |
| 30 | + | ||
| 31 | + | ``` pascal | |
| 32 | + | {$mode objfpc}{$H+} | |
| 33 | + | var | |
| 34 | + | MyFile: file of Single; | |
| 35 | + | Data: Single; | |
| 36 | + | ||
| 37 | + | begin | |
| 38 | + | AssignFile(MyFile, 'singlevalues.bin'); | |
| 39 | + | try | |
| 40 | + | Reset(MyFile); | |
| 41 | + | Read(MyFile, Data); | |
| 42 | + | ||
| 43 | + | finally | |
| 44 | + | CloseFile(MyFile); | |
| 45 | + | end; | |
| 46 | + | end. | |
| 47 | + | ``` | |
| 48 | + | ||
| 49 | + | ## Text file | |
| 50 | + | ||
| 51 | + | The type [[Text|TextFile]] (older, equivalent name: Text) represents a text file. Note that this is different than just **file of char**. There are various comfortable read and write operations available for text files, that parse integers, floating-point values and other types. Also, line endings are properly dealt with when you use text files. | |
| 52 | + | ||
| 53 | + | See [[Text|TextFile]] page for more information and examples. | |
| 54 | + | ||
| 55 | + | # Working with files | |
| 56 | + | ||
| 57 | + | Working with all file types is similar: | |
| 58 | + | ||
| 59 | + | 1. Associate a file with data. *Usually* by calling [[AssignFile]]. An older, equivalent name is [[Assign]]. (With the advent of GUI libraries there arose the possibility of name clashes with the names of some traditional Pascal file routines. TPersistent and TForm (a TPersistent descendent) have Assign and Close methods. Because of this, confusion can arise if you write Assign or Close in TForm methods, hence AssignFile and CloseFile were introduced as aliases for the traditional Assign and Close, to prevent inadvertently calling an unintended procedure. In non-GUI code you may of course use the original Assign and Close, and save a bit of typing). | |
| 60 | + | 2. Open the file by [[Reset]] (read existing file, and maybe write --- see below), [[Rewrite]] (clear contents and write), or [[Append]] (keep contents and write). In case of [[Reset]] on an untyped binary file, the global [[FileMode]] variable determines if you can only read (fmOpenRead), or both read and write (fmOpenReadWrite), to the file. | |
| 61 | + | 3. Then read/write file data by [[Read|read]], [[readln]], [[Write|write]] and [[writeln]] standard procedures. Some Pascal Compilers also used the [[Get]] and [[Put]] routines for I/O. For untyped binary files, use [[BlockRead]] and [[BlockWrite]]. | |
| 62 | + | 4. At the end, release the necessary file resources (and possibly flush the data to be written) by calling [[CloseFile]] (older, equivalent name: just [[Close]]). | |
| 63 | + | ||
| 64 | + | A file variable is used either for input, for output, or for both input and output (I/O). A file may be a screen and/or keyboard, a modem, a light pen, a touch screen, a mouse, an area of storage on disk, a socket for connecting to the Internet, or it could be some other device depending on how the run-time library ties the file. In some cases, actual memory can be defined as a file; this was used in the case of a ram disk. You can customize where the file data is by initializing [FileRec record](http://www.freepascal.org/docs-html/rtl/sysutils/filerec.html) (for untyped binary files) or [TextRec record](http://www.freepascal.org/docs-html/rtl/sysutils/textrec.html) (for text files), instead of calling standard [[AssignFile]] ([[Assign]]) routines. | |
| 65 | + | ||
| 66 | + | # See also | |
| 67 | + | ||
| 68 | + | - [[File Handling In Pascal]] | |
| 69 | + | ||
| 70 | + | --- | |
| 71 | + | *From the Free Pascal wiki, page "File", as it stood on 2020-02-14 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).* |
