RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
System.Write Function

Writes to either a typed file or a text file

Pascal
procedure Write(P1); overload;
procedure Write(P1; Pn); overload;
procedure Write(var F: Text; P1); overload;
procedure Write(var F: Text; P1; Pn); overload;
C++
Write( P1);
Write( P1,  Pn);
Write(Text F,  P1);
Write(Text F,  P1,  Pn);

System

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 Delphi 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 Delphi 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 followed by the character value of OutExpr is written.  

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's 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 11, it's assumed to be 11. The format of the fixed-point string follows:

 [<blanks>] [ - ] <digits> [.<decimals>]

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 larger 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.
 

Delphi Examples: 

 

{
This example reads a file one character at a time and writes
it into a file of your chosing.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  F1, F2: TextFile;
  Ch: Char;
begin
  if OpenDialog1.Execute then begin
    AssignFile(F1, OpenDialog1.Filename);
    Reset(F1);
    if SaveDialog1.Execute then begin
      AssignFile(F2, SaveDialog1.Filename);
      Rewrite(F2);
      while not Eof(F1) do
      begin
        Read(F1, Ch);
        Write(F2, Ch);
      end;
      CloseFile(F2);
    end;
    CloseFile(F1);
  end;
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!