Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PChar to String conversion

1 view
Skip to first unread message

Graham Harris

unread,
Apr 3, 2001, 2:51:57 PM4/3/01
to
Try

Meno1.Text := p

HTH

Graham Harris

Glynn

unread,
Apr 3, 2001, 2:54:53 PM4/3/01
to
Unless you're using D1, there is no need to use PChars for this purpose. An
AnsiString can be considered a PChar for many purposes. In this case, you're not
allocating any room for what you want to read. Try this -

SetLength(S,50);
FileStream.Read(S[1], 50);

hth, glynn


Mark Machen wrote:

> I know there's an easy answer to this, so forgive me for the question. I am
> trying to read in portions of text from a file into a TMemo. Ideally I
> would do this:
>
> var
> p: PChar;
> s: String;
>
> FileStream.Read(p^, 50);
>
> //this doesn't work
> Memo1.Lines.Add(p);
>
> //I don't this this does either
> Memo1.Lines.Add(p^);
>
> Also, is there a way to do this:
>
> FileStream.Read(s, 50); //generates an error

Iman L Crawford

unread,
Apr 3, 2001, 3:06:56 PM4/3/01
to
I couldn't tell from you example if you are allocating space for the PChar,
if not make sure you do. Then make sure it is null terminated before you
try to convert to string.

Also just assigning a string := pchar should do the typecasting.

or

setLength(somestring, len)
FileStream(pchar(somestring), len)

--
Iman
"I was looking handsome,
she was looking like an erotic vulture" The Pixies

Ralph Friedman (TeamB)

unread,
Apr 3, 2001, 3:17:47 PM4/3/01
to
Mark,

in article <3aca1a70$1_2@dnews>, you wrote:

> Also, is there a way to do this:
>
> FileStream.Read(s, 50); //generates an error
>

FileStream.Read(s[1], 50)

--
Regards
Ralph (TeamB)
===

Mark Machen

unread,
Apr 3, 2001, 2:42:29 PM4/3/01
to
I know there's an easy answer to this, so forgive me for the question. I am
trying to read in portions of text from a file into a TMemo. Ideally I
would do this:

var
p: PChar;
s: String;

FileStream.Read(p^, 50);

//this doesn't work
Memo1.Lines.Add(p);

//I don't this this does either
Memo1.Lines.Add(p^);

Also, is there a way to do this:

Shane

unread,
Apr 3, 2001, 3:05:56 PM4/3/01
to
procedure TForm1.Button1Click(Sender: TObject);
var
t: TFileStream;
s: String;
begin
t := TFileStream.Create(ExtractFilePath(Application.ExeName)+'test.txt',
fmOpenRead);
try
SetLength(s, 50);
t.Read(s[1], 50);
Memo1.Lines.Add(s);
finally
t.Free;
end;
end;

Shane


Rudy Velthuis (TeamB)

unread,
Apr 3, 2001, 4:16:53 PM4/3/01
to
In article <3aca1a70$1_2@dnews>, ma...@producingfaith.org says...

> //this doesn't work
> Memo1.Lines.Add(p);

It does.



> //I don't this this does either
> Memo1.Lines.Add(p^);

It shouldn't.



> Also, is there a way to do this:
>
> FileStream.Read(s, 50); //generates an error

SetLength(S, 50);
FileStream.Read(S[1], 50);

or

SetLength(S, 50);
FileStream.Read(PChar(S)^, 50);

--
Rudy Velthuis (TeamB)

Mark Machen

unread,
Apr 3, 2001, 3:56:48 PM4/3/01
to
Thanks all.

I didn't realize that f.Read(s[1], 50) would work like that. It looks like
you are only trying to fill that one character - not the string. Thanks
again.


Eduardo Gamboa

unread,
Apr 9, 2001, 2:03:30 PM4/9/01
to
If you want to convert a PChar to String, You can use StrPas.
var
s: String;
p: PChar
...
s := StrPas(p);

Eduardo
Quito-Ecuador

"Mark Machen" <ma...@producingfaith.org> wrote in message
news:3aca1a70$1_2@dnews...


> I know there's an easy answer to this, so forgive me for the question. I
am
> trying to read in portions of text from a file into a TMemo. Ideally I
> would do this:
>
> var
> p: PChar;
> s: String;
>
> FileStream.Read(p^, 50);
>

> file://this doesn't work
> Memo1.Lines.Add(p);
>
> file://I don't this this does either


> Memo1.Lines.Add(p^);
>
> Also, is there a way to do this:
>

> FileStream.Read(s, 50); file://generates an error
>
>
>
>


Father - Mike Johnson

unread,
Apr 9, 2001, 2:15:33 PM4/9/01
to
Try

memo1.lines.loadfromstream(Filestream);

gotta love streams!

Father

Eduardo Gamboa <mega...@uio.satnet.net> wrote in message
news:3ad1f934_1@dnews...


> If you want to convert a PChar to String, You can use StrPas.
> var
> s: String;
> p: PChar

> ....

Marc Griffin

unread,
Apr 10, 2001, 8:52:00 AM4/10/01
to
Delphi's String / pChar distinction is very blurred. Make sure you have
"Huge Strings" enabled in project options...

Use typecasting where possible: pcharvar := pChar(stringvar); or stringvar
:= String(pcharvar);
Make sure you have the memory allocated to the pChar, or use an array of
type Array[0..x] Of Char;

Eduardo Gamboa wrote in message <3ad1f934_1@dnews>...

0 new messages