Write: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Write
| Old | New | Line | |
|---|---|---|---|
| 1 | + | The procedures `write` and `writeLn` store a date in a [[Text|text]] or typed [[File|file]]. They are defined as part of the [[Standard Pascal|Pascal]] programming language, thus everyone can expect them to work no matter which [[Compiler|compiler]] is used. | |
| 2 | + | ||
| 3 | + | In [[Property|property]] definitions the [[Reserved word|reserved word]] `write` is used to direct write access. This article deals with the procedures `write` and `writeLn`. See [[Object|object]] and related articles for the occurrence of `write` in the context of properties. | |
| 4 | + | ||
| 5 | + | ## Behavior | |
| 6 | + | ||
| 7 | + | ### Signature | |
| 8 | + | ||
| 9 | + | `Write` as well as `writeLn` share almost the same identical formal signature. However a formal signature is omitted here, since you can not state their signatures in Pascal . Therefore a description follows: | |
| 10 | + | ||
| 11 | + | As an optional first parameter a `text` variable can be specified where data are written to. `Write` is additionally capable of writing to a [[typed files|typed file variable]] (`file of recordType`). If no destination is specified, [[Output|output]] is assumed. | |
| 12 | + | ||
| 13 | + | Thereafter any number of variables can be specified, but in the case of `write` at least one has to be present. They have to be [[Char|char]], [[Integer|integer]], [[Real|real]], [[String|string]], or any other data type that can be rendered as (a sequence of) character(s) via implicit typecasts ([[Operator overloading|operator overloading]]). In the case of typed files as destination, only variables of the file’s record type can be specified. | |
| 14 | + | ||
| 15 | + | If the destination is a `text` file, each data variable identifier may be followed by a [[Colon|colon]] and a non-negative integer value. This [[Basic Pascal Tutorial/Chapter 2/Formatting output|value specifies]] the minimum width in characters the representation of the respective variable will acquire. It will be padded with space characters, so it becomes right-justified. In [[Mode iso|{$mode ISO}]] and [[Mode extendedpascal|{$mode extendedPascal}]] this value specifies the *exact* width of `Boolean`, `char` and `string` values, thus `'X':0` will emit nothing. | |
| 16 | + | ||
| 17 | + | Floating-point variables may have another colon and non-negative integer value followed, thus two in total, that will specify the number of decimal places after the decimal-period. Also, by the specifying the second format specifier, the default scientific notation is turned off. | |
| 18 | + | ||
| 19 | + | ### Execution | |
| 20 | + | ||
| 21 | + | Calling `write`/`writeLn` will write the variables’ values to the destination, and if a `text` variable is the destination, possibly convert them into a representation suitable for humans before doing so. | |
| 22 | + | ||
| 23 | + | If the destination file is not open, the [[runtime error|run-time error]] 103 “File not open” will stop program execution. This RTE may be converted to an [`eInOutErrorr`](https://www.freepascal.org/docs-html/rtl/sysutils/einouterror.html) exception if the [[sysutils|sysUtils unit]] is included (via a [[Uses|uses-clause]]). However, if the destination file is `output`, no error may be raised at all. You simply will not see any output emitted by `write`/`writeLn` calls. | |
| 24 | + | ||
| 25 | + | ### Representation | |
| 26 | + | ||
| 27 | + | If the destination is a `text` file, all ordinal type arguments are converted to human-readable representation. Strings and characters are already considered to be human-readable regardless of their value, e. g. control characters will be written directly without conversion. | |
| 28 | + | ||
| 29 | + | Decimal representations of floating-point values may be rounded. All numerical types may be preceded by a negative sign, but a positive sign is never printed. | |
| 30 | + | ||
| 31 | + | `write` will try to convert the value of enumerated types into their canonical names. If such does not exist the [[runtime error|run-time error]] 107 “invalid enumeration” occurs. | |
| 32 | + | ||
| 33 | + | ``` pascal | |
| 34 | + | program writeDemo(input, output, stderr); | |
| 35 | + | ||
| 36 | + | type | |
| 37 | + | direction = (left, straightOn, right); | |
| 38 | + | ||
| 39 | + | var | |
| 40 | + | heading: direction; | |
| 41 | + | begin | |
| 42 | + | // 15 characters in total (including period) | |
| 43 | + | // 6 places after period | |
| 44 | + | // rounded | |
| 45 | + | writeLn(pi():15:6); | |
| 46 | + | ||
| 47 | + | heading := straightOn; | |
| 48 | + | // heading as enumeration will be left-aligned | |
| 49 | + | // but still use 15 characters | |
| 50 | + | writeLn(heading:15, '.'); | |
| 51 | + | end. | |
| 52 | + | ``` | |
| 53 | + | ||
| 54 | + | > **Note:** The default style of formatting numbers may differ depending on whether [[Mode iso|{$mode ISO}]] is chosen. | |
| 55 | + | ||
| 56 | + | ### Difference between `write` and `writeLn` | |
| 57 | + | ||
| 58 | + | `writeLn` will automatically write a [[End of Line|line feed]] after all other data variables (if any). This line feed is the one suitable for the platform the program runs on. Remember, the notion of “line” applies only for `text` files. | |
| 59 | + | ||
| 60 | + | ## See also | |
| 61 | + | ||
| 62 | + | - [`system.write`](https://www.freepascal.org/docs-html/rtl/system/write.html) and [`system.writeLn`](https://www.freepascal.org/docs-html/rtl/system/writeln.html) | |
| 63 | + | - [[Why use Pascal|Why use Pascal, § “the readLn and writeLn effect”]] | |
| 64 | + | - [[Read|read]] performs the opposite action | |
| 65 | + | - [[WriteStr|writeStr]] works like `write` but outputs into a string | |
| 66 | + | ||
| 67 | + | --- | |
| 68 | + | *From the Free Pascal wiki, page "Write", as it stood on 2023-01-25 after a change by Kai Burghardt, 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).* | |
| 69 | + | Category: Code |
