procedure Truncate ( var FileHandle : File ) ;
Description
The Truncate procedure truncates a file at the current file position. All data after the current file position is erased.
The file must have been assigned using Assign and opened using RewRite or Reset.
Text files are not supported.
Related commands
Append Open a text file to allow appending of text to the end
Erase Erase a file
FilePos Gives the file position in a binary or text file
Example code : Write to a file, then truncate it
var
myWord, myWord1, myWord2 : Word;
myFile : File of Word;
begin
// Try to open the Test.bin binary file for writing to
AssignFile(myFile, 'Test.cus');
ReWrite(myFile);
// Write a couple of Words to the file
myWord1 := 234;
myWord2 := 567;
Write(myFile, myWord1, myWord2);
// Close the file
CloseFile(myFile);
// Display the file contents
Reset(myFile);
ShowMessage('Before truncate :');
while not Eof(myFile) do
begin
Read(myFile, myWord);
ShowMessage(IntToStr(myWord));
end;
// Close, reopen, and truncate after the first word
CloseFile(myFile);
FileMode := 2;
Reset(myFile);
Read(myFile, myWord);
Truncate(myFile);
CloseFile(myFile);
// Display the file contents again
Reset(myFile);
ShowMessage('After truncate :');
while not Eof(myFile) do
begin
Read(myFile, myWord);
ShowMessage(IntToStr(myWord));
end;
// Close the file for the last time
CloseFile(myFile);
end;
Show full unit code
Before truncate :
234
567
After truncate :
234