System.Write
Object Pascal
procedure Write([var F: File]; P1; [ ..., PN]); overload;
procedure Write([var F: File]; P1; [ ..., PN]); overload;
Properties
Type | Visibility | Source | Unit | Parent |
---|---|---|---|---|
procedure | public | System.pas | System | System |
Description
Writes to either a typed file or a text file.
The syntax shown here for the Write procedure is an example that illustrates that Write can take a variable number of arguments.
Write procedure for typed files
In Object Pascal code, Write writes a file to a file component. F
is a file variable, and each V
is a variable of the same type as the component type of F
. For each variable written, the current file position is advanced to the next component. If the current file position is at the end of the file (that is, if Eof(F) is True), the file is expanded.
Write procedure for text files
In Object Pascal code, Write writes one or more values to a text file. F
, if specified, is a text file variable. If F
is omitted, the standard file variable Output
is assumed. Each P
is a write parameter. Each write parameter includes an output expression whose value is to be written to the file. A write parameter can also contain the specifications of a field width and a number of decimal places. Each output expression must be of a type Char, one of the Integer types (Byte, Shortint, Word, Longint, Cardinal), one of the floating-point types (Single, Real, Double, Extended, Currency), one of the string types (PChar, AnsiString, ShortString), a packed string, or one of the Boolean types (Boolean, Bool).
A write parameter has the form:
OutExpr [: MinWidth [: DecPlaces ] ]
where OutExpr is an output expression.
MinWidth and DecPlaces are type integer expressions:
- MinWidth specifies the minimum field width, which must be greater than 0. Exactly MinWidth characters are written (using leading blanks if necessary) except when OutExpr has a value that must be represented in more than MinWidth characters. In that case, enough characters are written to represent the value of OutExpr. Likewise, if MinWidth is omitted, then the necessary number of characters is written to represent the value of OutExpr.
- DecPlaces specifies the number of decimal places in a fixed-point representation of one of the Real types. It can be specified only if OutExpr is one of the Real types, and if MinWidth is also specified. When MinWidth is specified, it must be greater than or equal to 0.
Write with a character-type value: If MinWidth is omitted, the character value of OutExpr is written to the file. Otherwise, MinWidth - 1 blanks are written, followed by the character value of OutExpr.
Write with one of the integer type values: If MinWidth is omitted, the decimal representation of OutExpr is written to the file with no preceding blanks. If MinWidth is specified and its value is larger than the length of the decimal string, enough blanks are written before the decimal string to make the field width MinWidth.
Write with one of the real type values: If OutExpr has one of the real type values, its decimal representation is written to the file. The format of the representation depends on the presence or absence of DecPlaces. If DecPlaces is omitted (or if it is present but has a negative value), a floating-point decimal string is written. If MinWidth is also omitted, a default MinWidth of 17 is assumed; otherwise, if MinWidth is less than 8, it's assumed to be 8. The format of the floating-point string is:
[ | - ] <digit> . <decimals> E [ + | - ] <exponent>
The following table lists the components of the output string.
Component | Meaning |
---|---|
[ | - ] |
" “ or “-”l, according to the sign of OutExpr |
<digit> |
Single digit, “0” only if OutExpr is 0 |
<decimals> |
Digit string of MinWidth-7 (but at most 10) digits |
E |
Uppercase [E] character |
[ + | - ] |
According to the sign of the exponent |
<exponent> |
Two-digit decimal exponent |
If DecPlaces is present, a fixed-point decimal string is written. If DecPlaces is larger than 216, it is assumed to be 216. The format of the fixed-point string is as follows:
[<blanks>] [ - ] <digits> [.<decimals>]
Note: The actual precision of real types is smaller than 216 digits. The Double type has about 16 decimal digits, and the Single type has about 8 decimal digits. The Object Pascal RTL fills the '0' character into these extra precision fields. For example:
writeln(Pi:1:20);
displays:
3.14159265358979312000
The following table lists the components of the fixed-point string:
Component | Meaning |
---|---|
[<blanks>] |
Blanks to satisfy MinWidth |
[-] |
If OutExpr is negative |
<digits> |
At least one digit, but no leading zeros |
[.<decimals>] |
Decimals if DecPlaces > 0 |
Write with one of the string-type values: If MinWidth is omitted, the string value of OutExpr is written to the file with no leading blanks. If MinWidth is specified, and its value is greater than the length of OutExpr, enough blanks are written before the decimal string to make the field width MinWidth.
Write with a packed string-type value: If OutExpr is of packed string type, the effect is the same as writing a string whose length is the number of elements in the packed string type.
Write with one of the Boolean type values: If OutExpr is of type Boolean, the effect is the same as writing the strings True or False, depending on the value of OutExpr.
Note: When using Write the file must be open for output.