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

MemoryStream to FileStrean

31 views
Skip to first unread message

Ed

unread,
Oct 31, 2002, 10:42:37 AM10/31/02
to
Hi, clueless newbie here not knowing where to turn...I'm trying to
document a program's component properties from the following snippets
of code that I stole.


var
I: Integer;
fs: TFileStream;
ms: TMemoryStream;
componentId: TComponent;
begin
fs := nil;
ms := nil;
try
fs := TFileStream.Create('ComponentProperties.txt', fmCreate);
ms := TMemoryStream.Create;
for I := ComponentCount - 1 downto 0 do
begin
componentID := Components[I];
ms.WriteComponent(componentId);
ms.Position := 0;
ObjectBinaryToText(ms, fs);
end;
finally
fs.Free;
ms.Free;
end;

When ms.Position is reset to 0, the same component property gets
written to the file over and over. I try to increment the ms.Position
with the value of ms.Size and the first two or three components get
written to the file properly but then I get an exception class
EReadError with "Invalid Stream Format".

Also, would this be the recommended design within the try..finally
block with all open streams?

Thanks for any help.

ed

aparimana

unread,
Nov 1, 2002, 7:26:12 AM11/1/02
to
i've never tried exactly what you're doing, but.....

1. call ms.clear before ms.WriteComponent, then you'll only ever have
the latest component in the memory stream.

2. your 'create' calls should be the other side of the 'try'

create
try
code code code
finally
free
end;

the reason for this is that you only want to call free if create
succeeded

3. also you should ideally have two nested try's... the way you have
it at the moment, if the filestream.create raises an exception, ms is
never created but ms.free is called

create fs
try
create ms
try
code code
finally
free ms
end;
finally
free fs
end;

ap

Ed

unread,
Nov 1, 2002, 4:11:34 PM11/1/02
to
Thanks, ms.Clear worked great. I don't, however, get a complete list
of the properties displayed in the object inspector. Is there any way
to get the complete list and possibly the events references?

ed


aparimana <a...@camart.ignore-this-bit.co.uk> wrote in message news:<l9s4su410idefrbto...@4ax.com>...


> i've never tried exactly what you're doing, but.....
>
> 1. call ms.clear before ms.WriteComponent, then you'll only ever have
> the latest component in the memory stream.
>

0 new messages