Delphi conversion problem...

19 views
Skip to first unread message

Eugene

unread,
Jun 23, 2004, 5:24:07 PM6/23/04
to

Hi,

I would like to find and replace a string in a text file.I found
this little code which belongs to TeamB/P. Below but i am bad at
converting pascal code to BCB.

procedure FileReplaceString(const FileName, searchstring, replacestring: string);
var
fs: TFileStream;
S: string;
begin
fs := TFileStream.Create(FileName, fmOpenread or fmShareDenyNone);
try
SetLength(S, fs.Size);
fs.ReadBuffer(S[1], fs.Size);
finally
fs.Free;
end;
S := StringReplace(S, SearchString, replaceString, [rfReplaceAll, rfIgnoreCase]);
fs := TFileStream.Create(FileName, fmCreate);
try
fs.WriteBuffer(S[1], Length(S));
finally
fs.Free;
end;
end;

I tried to convert it like :

void FileReplaceString(AnsiString FileName, AnsiString SearchString,AnsiString ReplaceString)
{
TFileStream *fs;
AnsiString S;

fs = new TFileStream(FileName, fmOpenRead || fmShareDenyNone);
try
{
fs->ReadBuffer(S.c_str(), fs->Size);
}
__finally
{
delete fs;
}
S = StringReplace(S, SearchString, ReplaceString,TReplaceFlags() << rfReplaceAll << rfIgnoreCase);
fs = new TFileStream(FileName, fmCreate);
try
{
fs->WriteBuffer(S.c_str(), S.Length());
}
__finally
{
delete fs;
}
}

Pfff, my code is rubbish i know :) Can anyone help me to find
my way please? How to convert this procedure to Borland C++
Builder ?

Eugene

Remy Lebeau (TeamB)

unread,
Jun 23, 2004, 5:37:15 PM6/23/04
to

"Eugene" <Eug...@dmx.de> wrote in message
news:40d9f4f7$1...@newsgroups.borland.com...

> I found this little code which belongs to TeamB/P.
> Below but i am bad at converting pascal code to BCB.

void __fastcall FileReplaceString(const AnsiString &FileName, const
AnsiString &SearchString, const AnsiString &ReplaceString)
{
TFileStream *fs = new TFileStream(FileName, fmOpenread |
fmShareDenyNone);
try
{
AnsiString S;
S.SetLength(fs->Size);


fs->ReadBuffer(S.c_str(), fs->Size);
}
__finally {
delete fs;
}
S = StringReplace(S, SearchString, ReplaceString, TReplaceFlags() <<
rfReplaceAll << rfIgnoreCase);

fs = new TFileStream->Create(FileName, fmCreate);


try {
fs->WriteBuffer(S.c_str(), S.Length());
}
__finally {
delete fs;
}
}


Gambit


Eugene

unread,
Jun 24, 2004, 3:07:51 AM6/24/04
to

Hi Remy,

Thank you for your great help.I changed your code
slightly.Have a look at it.

Eugene

void __fastcall FileReplaceString(const AnsiString &FileName, const AnsiString &SearchString, const AnsiString &ReplaceString)
{

TFileStream *fs = new TFileStream(FileName, fmOpenRead | fmShareDenyNone);
AnsiString S;
try
{


S.SetLength(fs->Size);
fs->ReadBuffer(S.c_str(), fs->Size);
}
__finally {
delete fs;
}
S = StringReplace(S, SearchString, ReplaceString, TReplaceFlags() << rfReplaceAll << rfIgnoreCase);

fs = new TFileStream(FileName, fmCreate);

Ivan Johansen

unread,
Jun 24, 2004, 4:24:25 AM6/24/04
to
If you use std::auto_ptr you can remove the __finally. Remember to
#include <memory>.

void __fastcall FileReplaceString(const AnsiString &FileName, const
AnsiString &SearchString, const AnsiString &ReplaceString)
{

std::auto_ptr<TFileStream> fs(new TFileStream(FileName, fmOpenread |
fmShareDenyNone));


AnsiString S;
S.SetLength(fs->Size);
fs->ReadBuffer(S.c_str(), fs->Size);

fs.reset();

S = StringReplace(S, SearchString, ReplaceString,
TReplaceFlags() << rfReplaceAll << rfIgnoreCase);

fs.reset(new TFileStream->Create(FileName, fmCreate));
fs->WriteBuffer(S.c_str(), S.Length());
}

Ivan Johansen

Reply all
Reply to author
Forward
0 new messages