RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
System.SetTextBuf Function

Assigns an I/O buffer to a text file.

Pascal
procedure SetTextBuf(var F: Text; var Buf); overload;
procedure SetTextBuf(var F: Text; var Buf; Size: Integer); overload;
C++
SetTextBuf(Text F,  Buf);
SetTextBuf(Text F,  Buf, int Size);

In Delphi code, SetTextBuf changes the text file F to use the buffer specified by Buf instead of F's internal buffer. F is a text file variable, Buf is any variable, and Size is an optional expression.  

Each Text file variable has an internal 128-byte buffer that buffers Read and Write operations. This buffer is adequate for most operations. However, heavily I/O-bound programs benefit from a larger buffer to reduce disk head movement and file system overhead.  

Size specifies the size of the buffer in bytes. If Size is omitted, SizeOf(Buf) is assumed. The new buffer remains in effect until F is next passed to AssignFile.  

SetTextBuf can be called immediately after Reset, Rewrite, and Append, but never apply it to an open file.  

When SetTextBuf is called on an open file once I/O operations have taken place, data could be lost because of the change of buffer.  

The Delphi runtime library does not ensure that the buffer exists for the entire duration of I/O operations on the file. A common error is to install a local variable as a buffer, then use the file outside the procedure that declared the buffer.  

Delphi Examples: 

 

{
This example reads a file into a buffer and then dumps
the buffer into a new file WOOF.DOG
}
procedure TForm1.Button1Click(Sender: TObject);
var
  F, FTwo: System.TextFile;
  Ch: Char;
  Buf: array[1..4096] of Char;  { 4K buffer }
begin
  if OpenDialog1.Execute then
  begin
    AssignFile(F, OpenDialog1.FileName);
    { Larger buffer for faster reads }
    System.SetTextBuf(F, Buf);
    Reset(F);
    { Dump text file into another file }
    AssignFile(FTwo, 'WOOF.DOG');
    Rewrite(FTwo);
    while not Eof(f) do
    begin
      Read(F, Ch);
      Write(FTwo, Ch);
    end;
    System.CloseFile(F);
    System.CloseFile(FTwo);
  end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!