🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.delphi.rtl.general
14 messages
14 total messages Started by Kim S Wed, 01 Feb 2006 14:17
Replace string in Binary file
#175
Author: Kim S
Date: Wed, 01 Feb 2006 14:17
3 lines
998 bytes
Can anybody suggest a way to replace a string in a binary file?
Regards,
Kim
Re: Replace string in Binary file
#178
Author: krisztian pinter
Date: Wed, 01 Feb 2006 14:25
5 lines
1132 bytes
On Wed, 01 Feb 2006 15:17:32 +0100, Kim S <ks@pumpkin-post.dk> wrote:

> Can anybody suggest a way to replace a string in a binary file?

same length? different length? small file? big file?
Re: Replace string in Binary file
#177
Author: Kim S
Date: Wed, 01 Feb 2006 14:27
9 lines
1444 bytes
krisztian pinter wrote:

> On Wed, 01 Feb 2006 15:17:32 +0100, Kim S <ks@pumpkin-post.dk> wrote:
> 
>> Can anybody suggest a way to replace a string in a binary file?
> 
> 
> same length? different length? small file? big file?
6 kb file size, original string length = new string length
Re: Replace string in Binary file
#180
Author: krisztian pinter
Date: Wed, 01 Feb 2006 14:43
27 lines
1698 bytes
On Wed, 01 Feb 2006 15:27:05 +0100, Kim S <ks@pumpkin-post.dk> wrote:


>>> Can anybody suggest a way to replace a string in a binary file?
>>   same length? different length? small file? big file?

> 6 kb file size, original string length = new string length

you are lucky :)

var
   F: file
   Buf: string;

...
   AssignFile(F, 'your file');
   Reset(F,1);
   SetLength(Buf, FileSize(F));
   BlockRead(F, Buf[1], Length(Buf));
   Buf := StringReplace(Buf, 'old', 'new');
   Seek(F, 0);
   BlockWrite(F, Buf[1], Length(Buf));
   CloseFile(F);


this is the essentials. you must add error detection. plus correct my
errors :)
Re: Replace string in Binary file
#179
Author: Kim S
Date: Wed, 01 Feb 2006 14:53
38 lines
2050 bytes
krisztian pinter wrote:

> On Wed, 01 Feb 2006 15:27:05 +0100, Kim S <ks@pumpkin-post.dk> wrote:
> 
> 
>>>> Can anybody suggest a way to replace a string in a binary file?
>>>
>>>   same length? different length? small file? big file?
> 
> 
>> 6 kb file size, original string length = new string length
> 
> 
> you are lucky :)
> 
> var
>   F: file
>   Buf: string;
> 
> ...
>   AssignFile(F, 'your file');
>   Reset(F,1);
>   SetLength(Buf, FileSize(F));
>   BlockRead(F, Buf[1], Length(Buf));
>   Buf := StringReplace(Buf, 'old', 'new');
>   Seek(F, 0);
>   BlockWrite(F, Buf[1], Length(Buf));
>   CloseFile(F);
> 
> 
> this is the essentials. you must add error detection. plus correct my
> errors :)
> 
SUPER! SIMPLE! Works exactly like I wanted. Thanks.
A small correction:
   Buf := StringReplace(Buf, 'old', 'new', [rfReplaceAll]);
Regards,
Kim
Re: Replace string in Binary file
#182
Author: na
Date: Wed, 01 Feb 2006 14:58
15 lines
1336 bytes
other possibility:


var fs:     TFileStream
     st:     String;
begin

      fs := TFileStream.Create('Path');
      SetLength(st,fs.Size);
      fs.Read(PChar(st)^,fs.Size);
      st :=  StringReplace(st,'old Str',
	    'new Str',[rfReplaceAll,rfIgnoreCase]);
      fs.Position  := 0;
      fs.Write(PChar(st)^,Length(st));
      fs.Destroy;
Re: Replace string in Binary file
#181
Author: krisztian pinter
Date: Wed, 01 Feb 2006 15:07
7 lines
1300 bytes
On Wed, 01 Feb 2006 15:53:53 +0100, Kim S <ks@pumpkin-post.dk> wrote:

> SUPER! SIMPLE! Works exactly like I wanted. Thanks.
> A small correction:
>    Buf := StringReplace(Buf, 'old', 'new', [rfReplaceAll]);

don't forget to add error detection!!
Re: Replace string in Binary file
#183
Author: Kim S
Date: Thu, 02 Feb 2006 09:04
25 lines
1947 bytes
na wrote:

> other possibility:
> 
> 
> var fs:     TFileStream
>     st:     String;
> begin
> 
>      fs := TFileStream.Create('Path');
>      SetLength(st,fs.Size);
>      fs.Read(PChar(st)^,fs.Size);
>      st :=  StringReplace(st,'old Str',
>         'new Str',[rfReplaceAll,rfIgnoreCase]);
>      fs.Position  := 0;
>      fs.Write(PChar(st)^,Length(st));
>      fs.Destroy;
Hmm. I must do something wrong. Neither of the 2 suggestions actually 
replace anything in the file. I only get an updated filestamp.
I have tried testing with Pos and found the 'old str' in the file so I 
suspect that the StringReplace function does not replace anything. This 
is my actual line:
st := StringReplace (st, 'DDDDDD', '020097', [rfReplaceAll,rfIgnoreCase]);
--
Kim
Re: Replace string in Binary file
#184
Author: na
Date: Thu, 02 Feb 2006 09:40
31 lines
2123 bytes
Kim S schrieb:
> na wrote:
> 
>> other possibility:
>>
>>
>> var fs:     TFileStream
>>     st:     String;
>> begin
>>
>>      fs := TFileStream.Create('Path');
>>      SetLength(st,fs.Size);
>>      fs.Read(PChar(st)^,fs.Size);
>>      st :=  StringReplace(st,'old Str',
>>         'new Str',[rfReplaceAll,rfIgnoreCase]);
>>      fs.Position  := 0;
>>      fs.Write(PChar(st)^,Length(st));
>>      fs.Destroy;
> Hmm. I must do something wrong. Neither of the 2 suggestions actually 
> replace anything in the file. I only get an updated filestamp.
> I have tried testing with Pos and found the 'old str' in the file so I 
> suspect that the StringReplace function does not replace anything. This 
> is my actual line:
> st := StringReplace (st, 'DDDDDD', '020097', [rfReplaceAll,rfIgnoreCase]);
> -- 
> Kim

StringReplace has problems with binary strings. - unfortunately
you must use Pos & Copy command in while loop.

jru
Re: Replace string in Binary file
#186
Author: krisztian pinter
Date: Thu, 02 Feb 2006 09:59
13 lines
1730 bytes
On Thu, 02 Feb 2006 10:40:02 +0100, na <no@com.de> wrote:

> StringReplace has problems with binary strings. - unfortunately
> you must use Pos & Copy command in while loop.


jeeeezus! i just looked its implementation, and it is a pretty ugly
procedure indeed! not only limited, but also low performance. i
thought it is a low-profile replacer.

what design decision led borland to provide procedure-pairs for most
string operations (like CompareStr and AnsiCompareStr), but only a
high profile implementation of StringReplace? this is crazy, man!
Re: Replace string in Binary file
#187
Author: Kim S
Date: Thu, 02 Feb 2006 10:11
40 lines
2314 bytes
na wrote:

> Kim S schrieb:
> 
>> na wrote:
>>
>>> other possibility:
>>>
>>>
>>> var fs:     TFileStream
>>>     st:     String;
>>> begin
>>>
>>>      fs := TFileStream.Create('Path');
>>>      SetLength(st,fs.Size);
>>>      fs.Read(PChar(st)^,fs.Size);
>>>      st :=  StringReplace(st,'old Str',
>>>         'new Str',[rfReplaceAll,rfIgnoreCase]);
>>>      fs.Position  := 0;
>>>      fs.Write(PChar(st)^,Length(st));
>>>      fs.Destroy;
>>
>> Hmm. I must do something wrong. Neither of the 2 suggestions actually 
>> replace anything in the file. I only get an updated filestamp.
>> I have tried testing with Pos and found the 'old str' in the file so I 
>> suspect that the StringReplace function does not replace anything. 
>> This is my actual line:
>> st := StringReplace (st, 'DDDDDD', '020097', 
>> [rfReplaceAll,rfIgnoreCase]);
>> -- 
>> Kim
> 
> 
> StringReplace has problems with binary strings. - unfortunately
> you must use Pos & Copy command in while loop.
> 
> jru
Yep. That was it. Now all works as expected. Thanks again.
--
Kim
Re: Replace string in Binary file
#188
Author: na
Date: Thu, 02 Feb 2006 10:57
20 lines
1640 bytes
> 
> StringReplace has problems with binary strings. - unfortunately
> you must use Pos & Copy command in while loop.

try this code:

procedure ReplaceDirect(const Old,New: String; var AStr: String);
var p,i: Integer;
begin
     if Old = New then Exit;
     if Length(Old) <> Length(New) then
	raise Exception.Create('old and new have different length.');
     p := Pos(Old,AStr);
     while p > 0 do begin
         for i := 1 to Length(Old) do
            AStr[i+p-1] := new[i];
         p := PosEx(Old,AStr,p);
     end;
end;
Re: Replace string in Binary file
#189
Author: Kim S
Date: Thu, 02 Feb 2006 11:46
23 lines
2000 bytes
na wrote:
> 
>>
>> StringReplace has problems with binary strings. - unfortunately
>> you must use Pos & Copy command in while loop.
> 
> 
> try this code:
> 
> procedure ReplaceDirect(const Old,New: String; var AStr: String);
> var p,i: Integer;
> begin
>     if Old = New then Exit;
>     if Length(Old) <> Length(New) then
>     raise Exception.Create('old and new have different length.');
>     p := Pos(Old,AStr);
>     while p > 0 do begin
>         for i := 1 to Length(Old) do
>            AStr[i+p-1] := new[i];
>         p := PosEx(Old,AStr,p);
>     end;
> end;
Thanks all, it's quite overwhelming!
Re: Replace string in Binary file
#190
Author: John O'Harrow
Date: Fri, 03 Feb 2006 11:41
36 lines
2313 bytes
As already mentioned, StringReplace is not suitable.  It will stop at the 
first #0 character.

The best option would be to use the Fastcode AnsiStringReplace function at 
http://fastcode.sourceforge.net/, which works with embedded #0's and is also 
approx 20x faster.

-- 
regards,
John

The Fastcode Project:
http://www.fastcodeproject.org/


"na" <no@com.de> wrote in message news:43e1e52a$1@newsgroups.borland.com...
>
>>
>> StringReplace has problems with binary strings. - unfortunately
>> you must use Pos & Copy command in while loop.
>
> try this code:
>
> procedure ReplaceDirect(const Old,New: String; var AStr: String);
> var p,i: Integer;
> begin
>     if Old = New then Exit;
>     if Length(Old) <> Length(New) then
> raise Exception.Create('old and new have different length.');
>     p := Pos(Old,AStr);
>     while p > 0 do begin
>         for i := 1 to Length(Old) do
>            AStr[i+p-1] := new[i];
>         p := PosEx(Old,AStr,p);
>     end;
> end; 
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads