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 |
// Full Unit code. // ----------------------------------------------------------- // You must store this code in a unit called Unit1 with a form // called Form1 that has an OnCreate event called FormCreate. unit Unit1; interface uses // The System unit does not need to be defined SysUtils, Forms, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} // Include form definitions procedure TForm1.FormCreate(Sender: TObject); 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; end.
|
Hide full unit code |
Before truncate :
234
567
After truncate :
234
|
|