Delphi如何更快地搜索二进制文件?

Ale*_* P. 4 delphi binaryfiles delphi-7

我有一个二进制文件(2.5 MB),我想找到这个字节序列的位置:CD 09 D9 F5.然后我想在这个位置后写一些数据,并用零覆盖旧数据(4 KB).

这是我现在的方式,但它有点慢.

ProcessFile(dataToWrite: string);
var
  fileContent: string;
  f: file of char;
  c: char;
  n, i, startIndex, endIndex: integer;
begin
  AssignFile(f, 'file.bin');
  reset(f);
  n := FileSize(f);
  while n > 0 do
  begin
    Read(f, c);
    fileContent := fileContent + c;
    dec(n);
  end;
  CloseFile(f);

  startindex := Pos(Char($CD)+Char($09)+Char($D9)+Char($F5), fileContent) + 4;
  endIndex := startIndex + 4088;

  Seek(f, startIndex);

  for i := 1 to length(dataToWrite) do
    Write(f, dataToWrite[i]);

  c := #0;
  while (i < endIndex) do
  begin
    Write(f, c); inc(i);
  end;

  CloseFile(f);
end;
Run Code Online (Sandbox Code Playgroud)

mjn*_*mjn 6

请参阅此答案:在delphi中快速读取/写入文件

一些选项是:

要搜索文件缓冲区,请参阅在给定字节序列开始的Stream中查找位置的最佳方法 - 一个答案提到了Boyer-Moore算法,用于快速检测字节序列.