Name

SetTextBuf Procedure

Syntax

procedure SetTextBuf(var F: TextFile; var Buffer);
procedure SetTextBuf(var F: TextFile; var Buffer; Size: Integer);

Description

The SetTextBuf procedure sets a new text buffer to use for future I/O. Buffer is the new file buffer, and Size is its size in bytes. If Size is omitted, SetTextBuf uses SizeOf(Buffer).

Binary files do not have buffers, so SetTextBuf works only for the TextFile type.

You can set the text buffer for any open TextFile. Do not set the buffer after you have already performed I/O, or you may lose the contents of the old buffer. If the buffer is dynamically allocated, do not free the buffer until after you have closed the file.

SetTextBuf is not a real procedure.

Tips and Tricks

Every TextFile has a small built-in buffer. For console I/O and other ordinary uses, the small buffer is adequate. If you find that file I/O is a performance bottleneck, try using a larger buffer.

Example

// Read a list of numbers from a file and return the sum.
function SumFile(const FileName: string): Double;
var
  Number: Double;
  F: TextFile;
  Buffer: array[0..8191] of Char;
begin
  Result := 0.0;
  AssignFile(F, FileName);
  Reset(F);
  try
    SetTextBuf(F, Buffer);
    while not SeekEof(F) do
    begin
      ReadLn(F, Number);
      Result := Result + Number;
    end;
  finally
    CloseFile(F);
  end;
end;

See Also

IOResult Function, Reset Procedure, Rewrite Procedure, TextFile Type

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.