Discussion:
xpost (Sorry) Can someone show me the fix for this please?
(too old to reply)
bok
2012-07-19 18:45:01 UTC
Permalink
Hello,

The line below of "S:=Reversestring(S)" causes a file full of "nul" to be written instead of S. How do I manipulate S and then correctly write the altered
string to n2.exe.

Thanks,
DBM
=========================================================

procedure TForm1.Button1Click(Sender: TObject);
var
readit,writeit: TFileStream;
s: string;
x:integer;
begin
readit := TFileStream.Create('n1.exe', fmOpenread);
writeit := TFileStream.Create('n2.exe', fmCreate or fmOpenWrite);
try
SetLength(s,readit.Size);
readit.Read(PChar(s)^, Length(s));
s:=reversestring(s); //<<<<<<-This line is incorrect
writeit.Write(PChar(s)^, Length(s));
finally
readit.Free;
writeit.Free;
end;
end;
Marco van de Voort
2012-07-19 19:45:05 UTC
Permalink
Post by bok
The line below of "S:=Reversestring(S)" causes a file full of "nul" to be written instead of S. How do I manipulate S and then correctly write the altered
string to n2.exe.
An empty string might be NIL. Dereferencing NIL might cause an exception.
Post by bok
Thanks,
DBM
=========================================================
procedure TForm1.Button1Click(Sender: TObject);
var
readit,writeit: TFileStream;
s: string;
x:integer;
begin
readit := TFileStream.Create('n1.exe', fmOpenread);
writeit := TFileStream.Create('n2.exe', fmCreate or fmOpenWrite);
try
SetLength(s,readit.Size);
if length(s)>0
Post by bok
readit.Read(PChar(s)^, Length(s));
s:=reversestring(s); //<<<<<<-This line is incorrect
writeit.Write(PChar(s)^, Length(s));
finally
readit.Free;
writeit.Free;
end;
end;
bok
2012-07-19 23:26:06 UTC
Permalink
Marco,

Thank you for your reply. To clarify- the entire n2.exe file is filled with 179,712 zeros {the exact length of n1.exe}. I wasn't referring to the reserved word "NIL". Rather; on examination of n2.exe with Notepad++ each of those zero bytes is denoted as "NUL" [Ascii charts reference ord(0)as both "NUL" and "NULL"]. If I run the program without the reversestring command a perfect copy of n1.exe is created as n2.exe as expected.

I am a hobbyist trying to learn Delphi for fun. I don't even know what "dereferencing NIL" means let alone how it is done. I have read many of your posts here and elsewhere and recognize you as a knowledgeable programmer. Can you possibly post [what I guess] is the one or two lines of code that would make this procedure work so that I can use this as a learning tool.

I have read the Delphi help on both streams and pointers as well as Googled the same, but even after trying a score of different things I have not figured this out- probably because I don't have a firm grasp of the fundamentals. I have purchased books on Delphi(most written at least a decade ago), but streams are barely touched on if mentioned at all in most of them.

Thank You,
DBM
Post by Marco van de Voort
An empty string might be NIL. Dereferencing NIL might cause an exception.
bok
2012-07-20 00:54:10 UTC
Permalink
Marco,

Well my lack of grasp of fundamentals was correct!!! After about a total of about six hours playing with streams and pointers I discovered that the problem had nothing to do with either!!!

The fix? Change "var s:string" to "var s:ansistring"

I am both aggravated and relieved at the same time.

Thanks Anyway

DBM

P.S. Though this was totally MY fault do you think that Delphi 'could'
or 'should' of provided some sort of "overloaded" functionality to
correctly process the string as an ansistring automagically?
Marco van de Voort
2012-07-20 12:27:59 UTC
Permalink
Post by bok
The fix? Change "var s:string" to "var s:ansistring"
Then next time mention you use an unicode version. Unicode versions (D2009
and later) are fundamentally different when it comes to strings.

Loading...