Board index » delphi » TmemoryStream writing and reading words

TmemoryStream writing and reading words

I am trying to write a word (var word) to a TMemoryStream and read it back.
The online documentation is not clear (to me at least) about what the buffer
parameter is in the read and write methods.
This is where I am at:

var
Myword: word;
MyStream: TMemoryStream

MyStream:=TMemoryStream.create;
MyStream.size:= 2;
Mystream.write(?????,2);    {write word}
MyStream.Position:=0;
MyStream.Read(????,2);     {read word}
MyStream.Free;

--
Thanks
Wayne

 

Re:TmemoryStream writing and reading words


Quote
"Wayne Burnett" <wa...@rainwiseNOSPAM.com> wrote in message

news:topvsi1nnqo146@corp.supernews.com...

Quote
> I am trying to write a word (var word) to a TMemoryStream and read it
back.
> The online documentation is not clear (to me at least) about what the
buffer
> parameter is in the read and write methods.
> This is where I am at:

> var
> Myword: word;
> MyStream: TMemoryStream

> MyStream:=TMemoryStream.create;
> MyStream.size:= 2;
> Mystream.write(?????,2);    {write word}
> MyStream.Position:=0;
> MyStream.Read(????,2);     {read word}
> MyStream.Free;

. . .
myStream := tMemoryStream.Create;
try
    myStream.Write (myWord, SizeOf (myWord));
    . . .
    myStream.Position := 0;
    myStream.Read (myWord, SizeOf (myWord));
    . . .
finally
    myStream.Free;
    end;
. . .

Re:TmemoryStream writing and reading words


In article <topvsi1nnqo...@corp.supernews.com>, "Wayne Burnett"

Quote
<wa...@rainwiseNOSPAM.com> writes:
>I am trying to write a word (var word) to a TMemoryStream and read it back.
>The online documentation is not clear (to me at least) about what the buffer
>parameter is in the read and write methods.
>This is where I am at:

A word is like an integer just use ...

TSrteam.Write(MyWord, SizeOf(Word));

TStream.Read(MyWord, SizeOf(MyWord));

I have a small help file about streams, e-mail me if you would like a copy.

Alan Lloyd
alangll...@aol.com

Re:TmemoryStream writing and reading words


On Wed, 29 Aug 2001 10:40:55 -0400, "Wayne Burnett"

Quote
<wa...@rainwiseNOSPAM.com> wrote:
>I am trying to write a word (var word) to a TMemoryStream and read it back.
>The online documentation is not clear (to me at least) about what the buffer
>parameter is in the read and write methods.

Wasn't clear to me at first either. What the heck is that
"var Buffer" thing - where's the type of the variable?

It's an untyped parameter, which means you can pass
any variable at all (and things will blow up or not
depending on whether you pass the right value for
the Size parameter.) So you just

[insert here the example Bruce posted]

and you're set.

Quote
>This is where I am at:

>var
>Myword: word;
>MyStream: TMemoryStream

>MyStream:=TMemoryStream.create;
>MyStream.size:= 2;

You don't need to set the size here, btw,
in fact it _may_ be better if you don't.
One of the things that makes streams
useful is the size expands automatically
when you write past the end. (_Reading_
beyond the end is an error, of course.)

Quote
>Mystream.write(?????,2);    {write word}
>MyStream.Position:=0;
>MyStream.Read(????,2);     {read word}
>MyStream.Free;

>--
>Thanks
>Wayne

David C. Ullrich

Other Threads