gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 15:21:10 25 4
gpt4 key购买 nike

我有一个二进制文件(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;

最佳答案

请参阅此答案:Fast read/write from file in delphi

一些选项是:

要搜索文件缓冲区,请参阅 Best way to find position in the Stream where given byte sequence starts - 一个答案提到了 Boyer-Moore algorithm 用于快速检测字节序列。

关于Delphi 如何更快地搜索二进制文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15661929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com