Board index » delphi » TStrings.SaveToStream & TStrings.LoadFromStream

TStrings.SaveToStream & TStrings.LoadFromStream

I just find that TStrings.SaveToStream & TStrings.LoadFromStream do the
different work with SaveToFile & LoadFromFile.

For example,

            Memo1.Lines.SaveToFile('FileName1');
            Memo1.Lines.LoadFromFile('FileName1');

            The Memo1's content is unchanged.

However,

            Memo1.Lines.SaveToStream(Stream1);
            Memo1.Lines.LoadFromStream(Stream1);

            The Memo1's Content got blank.

I try to trace the source code.  I find that I should write
'Stream1.position:=0;' before LoadFromStream.  I don't understand why I
should do that. And if so, why the Help document do not recommand? (Delphi
3)

And I am confused with Stream1.Position:=0 and Stream1.Seek(0,0),  do they
get the same result?

Anyone would like the share the reason with me?  Thanks a lot.

Ajax.

 

Re:TStrings.SaveToStream & TStrings.LoadFromStream


On Mon, 4 Jan 1999 14:21:58 +0800, "ajax"

Quote
<clo...@sun1.mis.nsysu.edu.tw> wrote:
>I try to trace the source code.  I find that I should write
>'Stream1.position:=0;' before LoadFromStream.  I don't understand why I
>should do that.

Streams are different from files, although they can be used to access
files. This reflects on functions such as SaveToStream and SaveToFile.
When you call TStrings.SaveToFile, the file is opened (or created, if
it doesn't exist), the data is written, and the file is closed.
TStrings.SaveToStream doesn't know enough about the specific TStream
type, however, to open and close it. Many TStream types don't exists
in a "closed" state. For example, TMemoryStream - once closed
(destroyed, actually), the memory is released and the data is lost.

For that reason, SaveToStream simply starts writing the data at the
current position in the TStream object passed to it, and advances the
position pointer.

---
Yorai Aminov (TeamB)
http://ourworld.compuserve.com/homepages/yaminov
(TeamB cannot answer questions received via email.
To contact me for any other reason remove nospam from my address)

Re:TStrings.SaveToStream & TStrings.LoadFromStream


Quote
In article <76pmlp$7o...@forums.borland.com>, Ajax wrote:
> I try to trace the source code.  I find that I should write
> 'Stream1.position:=0;' before LoadFromStream.  I don't understand why I
> should do that.

Because that is how streaming works, allowing you to write multiple
entries to the stream.

 Mike Orriss (m...@3kcc.co.uk)
 http://www.3kcc.co.uk/notetree.htm

Other Threads