Solved

search and overwrite binary files data

Posted on 2003-10-23
6
183 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now