DelphiBasics
Writeln
Procedure
Write a complete line of data to a text file System unit
1 procedure Writeln(Expression1 {options} {, Expression2 {options} ...});
2 procedure Writeln (var FileHandle TextFile; Expression1 {options} {, Expression2 {options} ...};
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
AppendOpen a text file to allow appending of text to the end
AssignFileAssigns a file handle to a binary or text file
BlockReadReads a block of data records from an untyped binary file
BlockWriteWrites a block of data records to an untyped binary file
FileDefines a typed or untyped file
ReadRead data from a binary or text file
ReadLnRead a complete line of data from a text file
ResetOpen a text file for reading, or binary file for read/write
ReWriteOpen a text or binary file for write access
TextFileDeclares a file type for storing lines of text
WriteWrite data to a binary or text file
 Download this web site as a Windows program.




 
Example code : Illustrating single, multiple and formatted text line writing
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;
Show full unit code
  Hello World
  
  22/7 = 3.14285714285714E:0000
  22/7 = 3.142857
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page