Board index » delphi » Q: Making streams out of text in memory?

Q: Making streams out of text in memory?

Can anyone tell me how to make a stream out of plain text in memory?

What I want to do is to take the text from a Memo object (Memo1.Text) and
make a stream out of it so I can in a convenient way sequentially pick one
character out at a time, like:

        MyStream.Read(TheText,1)

I would appreciate any comment on this,(preferably via email).

Thanks in advance,

 - Fridrik

--
---------------------------------------------------------------------------
Fridrik Gudjon Gudnason   (P.O.Box 3163, IS-123)   E-Mail:frid...@rhi.hi.is
University of Iceland                              PGP available via finger
                    URL=http://www.rhi.hi.is/~fridrik

 

Re:Q: Making streams out of text in memory?


Quote
On Tuesday, 21 May 1996, Fridrik Gudjon Gudnason wrote...

> Can anyone tell me how to make a stream out of plain text in memory?

> What I want to do is to take the text from a Memo object (Memo1.Text)
and
> make a stream out of it so I can in a convenient way sequentially pick
one
> character out at a time, like:

>    MyStream.Read(TheText,1)

> I would appreciate any comment on this,(preferably via email).

Try this:

MyStream.Write( PChar(Memo1.Text)^, Length(Memo1.Text) );
MyStream.Position := 0;

Re:Q: Making streams out of text in memory?


Quote
David Wann wrote:

> On Tuesday, 21 May 1996, Fridrik Gudjon Gudnason wrote...

> > Can anyone tell me how to make a stream out of plain text in memory?

> > What I want to do is to take the text from a Memo object (Memo1.Text)
> and
> > make a stream out of it so I can in a convenient way sequentially pick
> one
> > character out at a time, like:

> >       MyStream.Read(TheText,1)

> > I would appreciate any comment on this,(preferably via email).

> Try this:

> MyStream.Write( PChar(Memo1.Text)^, Length(Memo1.Text) );
> MyStream.Position := 0;

        If it turns out you can't just typecast a string to a PChar like that
you could try this:

var MyStream:TMemoryStream;
begin
MyStream:=TMemoryStream.Create;
MyStream.SetSize(Memo1.GetTextLen);
Memo1.GetTextBuf(MyStream.Memory,Memo1.GetTextLen);
MyStream.Free;
end;

        That's if it has to be a stream for some reason - as long as the memo can't hold
more than 32K or whatever anyway, it seems like you could just as well use a PChar:

var P:PChar;
begin
Memo1.Text:='notepad';
P:=StrAlloc(Memo1.GetTextLen+1);
Memo1.GetTextBuf(P,Memo1.GetTextLen+1);
WinExec(P,SW_SHOWNORMAL);
StrDispose(P);
end;

(The point to the WinExec here is to show why you need the +1: GetTextLen does not
include the terminating null...)

--
David Ullrich
Sig file accidentally deleted - sorry.

Re:Q: Making streams out of text in memory?


Quote
>         If it turns out you can't just typecast a string to a PChar like that
> you could try this:

        Actually under D2, you can.

Re:Q: Making streams out of text in memory?


Quote
Chad Z. Hower wrote:

> >         If it turns out you can't just typecast a string to a PChar like that
> > you could try this:

>         Actually under D2, you can.

        Thanks. (Good thing I included the "if" part<g>... just now got Windows 95
running, D2 is next on the list.)

--
David Ullrich
Sig file accidentally deleted - sorry.

Other Threads