How can I find all instances of a string in a file , replace all instance of the string and save the copy of the file in Delphi
maybe, needs test about memory usage, you can
open file as "bytes"
and in a looping, search your byte sequences!
for example using "MemoryStream" search for "xx" bytes .... try find "Bitmap" signature on stream
type
TMyBuffer = array [0 .. 1] of byte;
function MyFindBitmapSignature(AMemoryStream: TStringStream): int64;
function MyIsTheSame(const ABuffer: TMyBuffer; const AFind: TMyBuffer): boolean; // better for many bytes comparing
begin
result := false;
//
if (Length(ABuffer) <> Length(AFind)) then
exit;
//
for var i: integer := 0 to (Length(ABuffer)-1) do
if (ABuffer[..i..] <> AFind[..i..]) then
exit(false);
//
result := true;
end;
var
LBuffer: TMyBuffer;
LFind : TMyBuffer;
begin
result := -1;
AMemoryStream.Position := 0;
LFind[0] := 66;
LFind[1] := 77;
//
for var i: int64 := 0 to ((AMemoryStream.Size div 2) - 1) do
begin
AMemoryStream.Read(LBuffer, 2);
//
if MyIsTheSame(LBuffer, LFind) then
exit(i * 2);
//
(*
if (LBuffer[0] = 66) and (LBuffer[1] = 77) then // 'nul, nul,... "BM" ...'
begin
exit(i * 2);
end;
*)
end;
end;