Solved

search and overwrite binary files data

Posted on 2003-10-23
6
185 Views
Last Modified: 2010-04-05
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
0
Comment
Question by:Tia86
6 Comments
 

Author Comment

by:Tia86
ID: 9608134
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!
0
 
LVL 3

Accepted Solution

by:
BJThomsen earned 43 total points
ID: 9608453
Simply copy the file from one file to another when you get to the string write the new string then the rest of the file.

This could result in corrupt data though. Not sure what the binary data is, but any positional references within the file could now be incorrect. If it was part of a structure the new format may not correctly read ect.

0
 
LVL 17

Assisted Solution

by:Wim ten Brink
Wim ten Brink earned 41 total points
ID: 9613446
Be very careful! Some binary formats depend on string values having a fixed length! There could be a length byte in front of the 'Hi' string or the system expects a fixed length and will consider 'llo' as part of some new data element instead as part of your 'Hello' string value. Especially executables are very sensitive about this. (And some executables maintain a checksum that will invalidate the exe if you modify data inside them.)
0
 
LVL 1

Assisted Solution

by:ttd
ttd earned 41 total points
ID: 9640460
Hi Tia86,

The answer is simply: No way!

Do you want to make your shin-bone 2 inches longer without "modifying" your leg? So modifying things is simple, but to keep the modified things working is not so simple unless you have a full knowledge about the thing that is being modified.

Regards,

ttd
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question