Link to home
Start Free TrialLog in
Avatar of Tia86
Tia86

asked on

search and overwrite binary files data

how i can replace a string into binary files BUT the 2 two strings haven't same length?

eg. old string = "Hi"
new string = "Hello"

example??

bye , tia86

125 points for the correct answer
Avatar of Tia86
Tia86

ASKER

i have found this code

procedure TForm1.Button1Click(Sender: TObject);
var
  f: file;
  l: Longint;
  FileName, oldstring, newstring, s: string;
begin
  oldstring := 'old_string';
  newstring := 'new_string';
  FileName  := 'file.xyz';
   
  s := oldstring;
  AssignFile(f, FileName);
  Reset(f, 1);
  for l := 0 to FileSize(f) - Length(oldstring) - 1 do
  begin
    Application.ProcessMessages;
    Seek(f, l);
    BlockRead(f, oldstring[1], Length(oldstring));
    if oldstring = s then
    begin
      Seek(f, l);
      BlockWrite(f, newstring[1], Length(newstring));
      ShowMessage('String successfully replaced!');
    end;
    Application.ProcessMessages;
  end;
  CloseFile(f);
end;


but don't work if the 2 strings haven't same lenght...

help me!
ASKER CERTIFIED SOLUTION
Avatar of BJThomsen
BJThomsen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Wim ten Brink
Wim ten Brink
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial