RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
System.Truncate Function

Deletes all the records after the current file position.

Pascal
procedure Truncate(var F);
C++
Truncate( F);

In Delphi code, Call Truncate to cause the current file position to become end-of-file (Eof(F) is true). 

F is a file variable of any type except a text file. Truncate does not work on text files. F must be open.  

Delphi Examples: 

 

{
This example shows how to truncate a file after the position
set by the last read statement.
}
procedure TForm1.Button1Click(Sender: TObject);
var
   f: file of Integer;
   i,j: Integer;
 begin
   AssignFile(f,'TEST.INT');
   Rewrite(f);
   for i := 1 to 6 do
     Write(f,i);
   Writeln('File before truncation:');
   Reset(f);
   while not Eof(f) do
   begin
     Read(f,i);
     Writeln(i);
   end;
   Reset(f);
   for i := 1 to 3 do
     Read(f,j); { Read ahead 3 records }
   Truncate(f); { Cut file off here }
   Writeln;
   Writeln('File after truncation:');
   Reset(f);
   while not Eof(f) do
   begin
     Read(f,i);
     Writeln(i);
   end;
   CloseFile(f);
   Erase(f);
 end;

 

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