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.