Gabr beat me, but I took a different approach:
procedure HeadCrop(const AFileName: string; const AHowMuch: Int64); var lFileStream: TFileStream; lPos: Int64; lRead: Integer; lBuffer: array[0..511] of Byte; begin lFileStream := TFileStream.Create(AFileName, fmOpenReadWrite); try if lFileStream.Size < AHowMuch then begin lFileStream.Size := 0; Exit; end; lPos := AHowMuch; repeat lFileStream.Position := lPos; lRead := lFileStream.Read(lBuffer, SizeOf(lBuffer)); lFileStream.Position := lPos - AHowMuch; lFileStream.Write(lBuffer, lRead); Inc(lPos, SizeOf(lBuffer)); until lRead <> SizeOf(lBuffer); lFileStream.Size := lFileStream.Size - AHowMuch; finally lFileStream.Free; end; end;
This code reads the file and moves blocks with 512 bytes to the file.
PS: this procedure only moves bytes, not characters! Thus, this only works for a single byte character.
The_fox
source share