Description |
The WriteLn procedure writes a complete line of data to a text file or to the console.
Version 1
Is used to write a line of text to the console.
Version 2
Is used to write a line of text to a text file with the given FileHandle.
You must use AssignFile to assign a file to the FileHandle and open the file with Reset or ReWrite before using WriteLn.
The text written may be any valid Expression(s). Often these will be strings, but may also be expressions that result in strings or numbers.
After each expression, you can add formatting options:
:width | Field width for strings + numbers |
:precision | Decimal digits for numbers |
|
|
Notes |
WriteLn does not buffer records, so performance is degraded. BlockWrite is more efficient, but is geared to writing to binary files.
|
|
Related commands |
Append |
|
Open a text file to allow appending of text to the end |
AssignFile |
|
Assigns a file handle to a binary or text file |
BlockRead |
|
Reads a block of data records from an untyped binary file |
BlockWrite |
|
Writes a block of data records to an untyped binary file |
File |
|
Defines a typed or untyped file |
Read |
|
Read data from a binary or text file |
ReadLn |
|
Read a complete line of data from a text file |
Reset |
|
Open a text file for reading, or binary file for read/write |
ReWrite |
|
Open a text or binary file for write access |
TextFile |
|
Declares a file type for storing lines of text |
Write |
|
Write data to a binary or text file |
|
|
|
Example code : Illustrating single, multiple and formatted text line writing |
// Full Unit code. // ----------------------------------------------------------- // You must store this code in a unit called Unit1 with a form // called Form1 that has an OnCreate event called FormCreate. unit Unit1; interface uses // The System unit does not need to be defined Forms, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} // Include form definitions procedure TForm1.FormCreate(Sender: TObject); var
myFile : TextFile;
text : string;
begin // Try to open the Test.txt file for writing to
AssignFile(myFile, 'Test.txt');
ReWrite(myFile);
// Write a couple of well known words to this file
WriteLn(myFile, 'Hello World');
// Write a blank line
WriteLn(myFile);
// Write a string and a number to the file
WriteLn(myFile, '22/7 = ' , 22/7);
// Repeat the above, but with number formatting
WriteLn(myFile, '22/7 = ' , 22/7:12:6);
// Close the file
CloseFile(myFile);
// Reopen the file for reading
Reset(myFile);
// Display the file contents
while not Eof(myFile) do
begin
ReadLn(myFile, text);
ShowMessage(text);
end;
// Close the file for the last time
CloseFile(myFile);
end; end.
|
Hide full unit code |
Hello World
22/7 = 3.14285714285714E:0000
22/7 = 3.142857
|
|