stream read error when copying from memory stream to file stream

DSOM
DSOM used Ask the Experts™
on
I have a tmemorystream and I want to copy it to a tfilestream but I am getting stream read error.

quick and dirty example:

new.stream:=tfilestream.Create('c:\temp.tmp', fmcreate);
new.stream.copyfrom(memstream, memstream.size);

Shouldn't this copy the contents of memstream to the filestream?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Martin BarredaPropietario asociado

Commented:
From Delphi Help: "fmCreate      - ... If a file with the given name exists, open the file in write mode."
I understand that if it do not exists, the function only creates it but do not open it.
You should call
tfilestream.Create('c:\temp.tmp', fmcreate AND fmOpenWrite);
or
tfilestream.Create('c:\temp.tmp', fmcreate OR fmOpenWrite);
Something like this.
Hope this help!!
Senior Developer
Commented:
make sure you do
  memstream.Seek(0, soFromBeginning);
before the
  new.Stream.CopyFrom(memstream, memstream.Size);
eg.

Author

Commented:
thanks TheRealLoki, that was the problem!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial