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
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
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.
>