Difference between revisions of "Write"
(adaption of read) |
|||
Line 1: | Line 1: | ||
{{Write}} | {{Write}} | ||
− | + | The procedures <syntaxhighlight lang="pascal" enclose="none">write</syntaxhighlight> and <syntaxhighlight lang="pascal" enclose="none">writeLn</syntaxhighlight> store a date in a [[Text|<syntaxhighlight lang="pascal" enclose="none">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 has been used. | |
− | < | + | In [[Property|<syntaxhighlight lang="pascal" enclose="none">property</syntaxhighlight>]] definitions the [[Reserved word|reserved word]] <syntaxhighlight lang="pascal" enclose="none">write</syntaxhighlight> is used to direct read access. |
− | + | This article deals with the procedures <syntaxhighlight lang="pascal" enclose="none">write</syntaxhighlight> and <syntaxhighlight lang="pascal" enclose="none">writeLn</syntaxhighlight>. | |
− | + | See [[Object|<syntaxhighlight lang="pascal" enclose="none">object</syntaxhighlight>]] and related articles for the occurrence of <syntaxhighlight lang="pascal" enclose="none">write</syntaxhighlight> in the context of properties. | |
− | + | ||
− | + | == behavior == | |
− | + | === signature === | |
− | + | <syntaxhighlight lang="pascal" enclose="none">Write</syntaxhighlight> as well as <syntaxhighlight lang="pascal" enclose="none">writeLn</syntaxhighlight> share almost the same identical formal signature. | |
− | + | 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: |
+ | As an optional first parameter a <syntaxhighlight lang="pascal" enclose="none">text</syntaxhighlight> variable can be specified where data are written to. | ||
+ | <syntaxhighlight lang="pascal" enclose="none">Write</syntaxhighlight> is additionally capable of writing to a typed <syntaxhighlight lang="pascal" enclose="none">file</syntaxhighlight> variable (<syntaxhighlight lang="pascal" enclose="none">file of recordType</syntaxhighlight>). | ||
+ | If no destination is specified, [[Output|<syntaxhighlight lang="pascal" enclose="none">output</syntaxhighlight>]] is assumed. | ||
+ | Thereafter any number of variables can be specified, but in the case of <syntaxhighlight lang="pascal" enclose="none">write</syntaxhighlight> at least one has to be present. | ||
+ | They have to be [[Char|<syntaxhighlight lang="pascal" enclose="none">char</syntaxhighlight>]], [[Integer|<syntaxhighlight lang="pascal" enclose="none">integer</syntaxhighlight>]], [[Real|<syntaxhighlight lang="pascal" enclose="none">real</syntaxhighlight>]], [[String|<syntaxhighlight lang="pascal" enclose="none">string</syntaxhighlight>]], 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. | ||
+ | If the destination is a <syntaxhighlight lang="pascal" enclose="none">text</syntaxhighlight> file, each data variable identifier may be followed by a [[Colon|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. | ||
+ | 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 <syntaxhighlight lang="pascal" enclose="none">write</syntaxhighlight>/<syntaxhighlight lang="pascal" enclose="none">writeLn</syntaxhighlight> will write the variables' values to the destination, and if a <syntaxhighlight lang="pascal" enclose="none">text</syntaxhighlight> variable is the destination, possibly convert them into a representation suitable for humans before doing so. | ||
+ | |||
+ | === representation === | ||
+ | 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. | ||
+ | |||
+ | <syntaxhighlight lang="pascal" enclose="none">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. | ||
− | + | <syntaxhighlight lang="pascal">program writeDemo(input, output, stderr); | |
− | + | type | |
+ | direction = (left, straightOn, right); | ||
− | + | var | |
+ | heading: direction; | ||
+ | begin | ||
+ | // 12 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.</syntaxhighlight> | ||
+ | === difference between <syntaxhighlight lang="pascal" enclose="none">write</syntaxhighlight> and <syntaxhighlight lang="pascal" enclose="none">writeLn</syntaxhighlight> === | ||
+ | <syntaxhighlight lang="pascal" enclose="none">writeLn</syntaxhighlight> 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 <syntaxhighlight lang="pascal" enclose="none">text</syntaxhighlight> files. | ||
− | + | == see also == | |
+ | * {{Doc|package=RTL|unit=system|identifier=write|text=<syntaxhighlight lang="pascal" enclose="none">system.write</syntaxhighlight>}} and {{Doc|package=RTL|unit=system|identifier=writeln|text=<syntaxhighlight lang="pascal" enclose="none">system.writeLn</syntaxhighlight>}} | ||
+ | * [[Why use Pascal#The Readln and Writeln effect|Why use Pascal, § “the <syntaxhighlight lang="pascal" enclose="none">readLn</syntaxhighlight> and <syntaxhighlight lang="pascal" enclose="none">writeLn</syntaxhighlight> effect”]] | ||
+ | * [[Read|<syntaxhighlight lang="pascal" enclose="none">read</syntaxhighlight>]] performs the opposite action | ||
+ | [[Category:Code]] | ||
[[Category:File Handling]] | [[Category:File Handling]] | ||
[[Category:Pascal]] | [[Category:Pascal]] |
Revision as of 13:54, 3 April 2019
│
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 has been used.
In property
definitions the reserved word write
is used to direct read 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.
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.
representation
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
// 12 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.
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 action