Difference between revisions of "Write"
(partially undo revision 134002 by Trev (talk); remove static “back to <some arbitrary page>” as there are _multiple_ pages linking here) |
m (bypass redirect to Basic Pascal Tutorial/Chapter 2/Formatting output, remove excessive space) |
||
(8 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
{{Write}} | {{Write}} | ||
+ | The procedures <syntaxhighlight lang="pascal" inline>write</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>writeLn</syntaxhighlight> store a date in a [[Text|<syntaxhighlight lang="pascal" inline>text</syntaxhighlight>]] 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. | ||
− | + | In [[Property|<syntaxhighlight lang="pascal" inline>property</syntaxhighlight>]] definitions the [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" inline>write</syntaxhighlight> is used to direct write access. | |
− | + | This article deals with the procedures <syntaxhighlight lang="pascal" inline>write</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>writeLn</syntaxhighlight>. | |
− | + | See [[Object|<syntaxhighlight lang="pascal" inline>object</syntaxhighlight>]] and related articles for the occurrence of <syntaxhighlight lang="pascal" inline>write</syntaxhighlight> in the context of properties. | |
− | In [[Property|<syntaxhighlight lang="pascal" | ||
− | This article deals with the procedures <syntaxhighlight lang="pascal" | ||
− | See [[Object|<syntaxhighlight lang="pascal" | ||
== Behavior == | == Behavior == | ||
− | |||
=== Signature === | === Signature === | ||
− | + | <syntaxhighlight lang="pascal" inline>Write</syntaxhighlight> as well as <syntaxhighlight lang="pascal" inline>writeLn</syntaxhighlight> share almost the same identical formal signature. | |
− | <syntaxhighlight lang="pascal" | ||
However a formal signature is omitted here, since you can not state their signatures in Pascal<!-- (i.e. theoretically unlimited list of parameters of various acceptable types, and an ''optional'' first parameter) -->. | However a formal signature is omitted here, since you can not state their signatures in Pascal<!-- (i.e. theoretically unlimited list of parameters of various acceptable types, and an ''optional'' first parameter) -->. | ||
Therefore a description follows: | Therefore a description follows: | ||
− | As an optional first parameter a <syntaxhighlight lang="pascal" | + | |
− | <syntaxhighlight lang="pascal" | + | As an optional first parameter a <syntaxhighlight lang="pascal" inline>text</syntaxhighlight> variable can be specified where data are written to. |
− | If no destination is specified, [[Output|<syntaxhighlight lang="pascal" | + | <syntaxhighlight lang="pascal" inline>Write</syntaxhighlight> is additionally capable of writing to a [[typed files|typed <syntaxhighlight lang="pascal" inline>file</syntaxhighlight> variable]] (<syntaxhighlight lang="pascal" inline>file of recordType</syntaxhighlight>). |
− | Thereafter any number of variables can be specified, but in the case of <syntaxhighlight lang="pascal" | + | If no destination is specified, [[Output|<syntaxhighlight lang="pascal" inline>output</syntaxhighlight>]] is assumed. |
− | They have to be [[Char|<syntaxhighlight lang="pascal" | + | |
− | In the case of typed files as destination, only variables of the | + | Thereafter any number of variables can be specified, but in the case of <syntaxhighlight lang="pascal" inline>write</syntaxhighlight> at least one has to be present. |
− | If the destination is a <syntaxhighlight lang="pascal" | + | They have to be [[Char|<syntaxhighlight lang="pascal" inline>char</syntaxhighlight>]], [[Integer|<syntaxhighlight lang="pascal" inline>integer</syntaxhighlight>]], [[Real|<syntaxhighlight lang="pascal" inline>real</syntaxhighlight>]], [[String|<syntaxhighlight lang="pascal" inline>string</syntaxhighlight>]], or any other data type that can be rendered as (a sequence of) character(s) via implicit typecasts ([[Operator overloading|operator overloading]]). |
− | This value specifies the minimum width in characters the representation of the respective variable will acquire. | + | In the case of typed files as destination, only variables of the file’s record type can be specified. |
+ | |||
+ | If the destination is a <syntaxhighlight lang="pascal" inline>text</syntaxhighlight> 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. | It will be padded with space characters, so it becomes right-justified. | ||
+ | In [[Mode iso|<syntaxhighlight lang="pascal" inline>{$mode ISO}</syntaxhighlight>]] and [[Mode extendedpascal|<syntaxhighlight lang="pascal" inline>{$mode extendedPascal}</syntaxhighlight>]] this value specifies the ''exact'' width of <syntaxhighlight lang="pascal" inline>Boolean</syntaxhighlight>, <syntaxhighlight lang="pascal" inline>char</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>string</syntaxhighlight> values, thus <syntaxhighlight lang="pascal" inline>'X':0</syntaxhighlight> will emit nothing. | ||
+ | |||
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. | 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. | Also, by the specifying the second format specifier, the default scientific notation is turned off. | ||
=== Execution === | === Execution === | ||
+ | Calling <syntaxhighlight lang="pascal" inline>write</syntaxhighlight>/<syntaxhighlight lang="pascal" inline>writeLn</syntaxhighlight> will write the variables’ values to the destination, and if a <syntaxhighlight lang="pascal" inline>text</syntaxhighlight> variable is the destination, possibly convert them into a representation suitable for humans before doing so. | ||
− | + | 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 {{Doc|package=RTL|unit=sysutils|identifier=einouterror|text=<syntaxhighlight lang="pascal" inline>eInOutErrorr</syntaxhighlight>}} exception if the [[sysutils|<syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> unit]] is included (via a [[Uses|<syntaxhighlight lang="pascal" inline>uses</syntaxhighlight>-clause]]). | ||
+ | However, if the destination file is <syntaxhighlight lang="pascal" inline>output</syntaxhighlight>, no error may be raised at all. | ||
+ | You simply will not see any output emitted by <syntaxhighlight lang="pascal" inline>write</syntaxhighlight>/<syntaxhighlight lang="pascal" inline>writeLn</syntaxhighlight> calls. | ||
=== Representation === | === Representation === | ||
+ | If the destination is a <syntaxhighlight lang="pascal" inline>text</syntaxhighlight> 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. | ||
Decimal representations of floating-point values may be rounded. | 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. | All numerical types may be preceded by a negative sign, but a positive sign is never printed. | ||
− | <syntaxhighlight lang="pascal" | + | <syntaxhighlight lang="pascal" inline>write</syntaxhighlight> 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. | If such does not exist the [[runtime error|run-time error]] 107 “invalid enumeration” occurs. | ||
Line 57: | Line 65: | ||
writeLn(heading:15, '.'); | writeLn(heading:15, '.'); | ||
end.</syntaxhighlight> | end.</syntaxhighlight> | ||
+ | {{Note|The default style of formatting numbers may differ depending on whether [[Mode iso|<syntaxhighlight lang="pascal" inline>{$mode ISO}</syntaxhighlight>]] is chosen.}} | ||
− | === Difference between <syntaxhighlight lang="pascal" | + | === Difference between <syntaxhighlight lang="pascal" inline>write</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>writeLn</syntaxhighlight> === |
− | + | <syntaxhighlight lang="pascal" inline>writeLn</syntaxhighlight> will automatically write a [[End of Line|line feed]] after all other data variables (if any). | |
− | <syntaxhighlight lang="pascal" | ||
This line feed is the one suitable for the platform the program runs on. | This line feed is the one suitable for the platform the program runs on. | ||
− | Remember, the notion of “line” applies only for <syntaxhighlight lang="pascal" | + | Remember, the notion of “line” applies only for <syntaxhighlight lang="pascal" inline>text</syntaxhighlight> files. |
== See also == | == See also == | ||
− | + | * {{Doc|package=RTL|unit=system|identifier=write|text=<syntaxhighlight lang="pascal" inline>system.write</syntaxhighlight>}} and {{Doc|package=RTL|unit=system|identifier=writeln|text=<syntaxhighlight lang="pascal" inline>system.writeLn</syntaxhighlight>}} | |
− | * {{Doc|package=RTL|unit=system|identifier=write|text=<syntaxhighlight lang="pascal" | + | * [[Why use Pascal#The Readln and Writeln effect|Why use Pascal, § “the <syntaxhighlight lang="pascal" inline>readLn</syntaxhighlight> and <syntaxhighlight lang="pascal" inline>writeLn</syntaxhighlight> effect”]] |
− | * [[Why use Pascal#The Readln and Writeln effect|Why use Pascal, § “the <syntaxhighlight lang="pascal" | + | * [[Read|<syntaxhighlight lang="pascal" inline>read</syntaxhighlight>]] performs the opposite action |
− | * [[Read|<syntaxhighlight lang="pascal" | + | * [[WriteStr|<syntaxhighlight lang="pascal" inline>writeStr</syntaxhighlight>]] works like <syntaxhighlight lang="pascal" inline>write</syntaxhighlight> but outputs into a string |
[[Category: Code]] | [[Category: Code]] |
Latest revision as of 03:58, 25 January 2023
│
Deutsch (de) │
English (en) │
español (es) │
русский (ru) │
The procedures write
and writeLn
store a date in a text
or typed file.
They are defined as part of the Pascal programming language, thus everyone can expect them to work no matter which compiler is used.
In property
definitions the reserved word write
is used to direct write access.
This article deals with the procedures write
and writeLn
.
See object
and related articles for the occurrence of write
in the context of properties.
Behavior
Signature
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:
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 file
variable (file of recordType
).
If no destination is specified, output
is assumed.
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
, integer
, real
, string
, or any other data type that can be rendered as (a sequence of) character(s) via implicit typecasts (operator overloading).
In the case of typed files as destination, only variables of the file’s record type can be specified.
If the destination is a text
file, each data variable identifier may be followed by a colon and a non-negative integer value.
This 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}
and {$mode extendedPascal}
this value specifies the exact width of Boolean
, char
and string
values, thus 'X':0
will emit nothing.
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.
Execution
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.
If the destination file is not open, the run-time error 103 “File not open” will stop program execution.
This RTE may be converted to an eInOutErrorr
exception if the sysUtils
unit is included (via a 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.
Representation
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.
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.
write
will try to convert the value of enumerated types into their canonical names.
If such does not exist the run-time error 107 “invalid enumeration” occurs.
program writeDemo(input, output, stderr);
type
direction = (left, straightOn, right);
var
heading: direction;
begin
// 15 characters in total (including period)
// 6 places after period
// rounded
writeLn(pi():15:6);
heading := straightOn;
// heading as enumeration will be left-aligned
// but still use 15 characters
writeLn(heading:15, '.');
end.
{$mode ISO}
is chosen.Difference between write
and writeLn
writeLn
will automatically write a 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.
See also
system.write
andsystem.writeLn
- Why use Pascal, § “the
readLn
andwriteLn
effect” read
performs the opposite actionwriteStr
works likewrite
but outputs into a string