Solved

search and overwrite binary files data

Posted on 2003-10-23
6
188 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

705 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