Solved

Append content of TMemoryStream to TFileStream

Posted on 2006-06-16
3
1,465 Views
Last Modified: 2008-02-01
Hi there.

I have a TFileStream and TMemoryStream.

What I want to do is

Open TMemoryStream - fill with something

Open TFileStream - write TMemoryStream to the end of TFileSream
Close TFileStream

Clear TMemoryStream

Someone got some pointers?
Thanks
0
Comment
Question by:wildzero
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 250 total points
ID: 16925338

This may sound redundant, by why use a memory stream when you are just appending data to the file stream? Anyways, here are 3 examples of peforming the operation you described (using slightly different techniques):

Regards,
Russell

var  strmMem:       TMemoryStream;
     strmFile:      TFileStream;
     szBuffer:      Array [0..127] of Char;
begin

  StrCopy(@szBuffer, 'Hello world'#13#10);

  // Example one - uses both a memory and file stream
  strmMem:=TMemoryStream.Create;
  try
     strmMem.Write(szBuffer, StrLen(@szBuffer));
     if FileExists('c:\test.txt') then
        strmFile:=TFileStream.Create('c:\test.txt', fmOpenReadWrite)
     else
        strmFile:=TFileStream.Create('c:\test.txt', fmCreate);
     try
        strmFile.Seek(0, soFromEnd);
        strmFile.CopyFrom(strmMem, 0);
     finally
        strmFile.Free;
     end;
  finally
     strmMem.Free;
  end;

  // Example two - uses just a file stream
  if FileExists('c:\test.txt') then
     strmFile:=TFileStream.Create('c:\test.txt', fmOpenReadWrite)
  else
     strmFile:=TFileStream.Create('c:\test.txt', fmCreate);
  try
     strmFile.Seek(0, soFromEnd);
     strmFile.Write(szBuffer, StrLen(szBuffer));
  finally
     strmFile.Free;
  end;

  // Example three - uses a memory stream to load file contents, modify, and then resave
  strmMem:=TMemoryStream.Create;
  try
     if FileExists('c:\test.txt') then strmMem.LoadFromFile('c:\test.txt');
     strmMem.Seek(0, soFromEnd);
     strmMem.Write(szBuffer, StrLen(szBuffer));
     strmMem.SaveToFile('c:\test.txt');
  finally
     strmFile.Free;
  end;

end;



0
 
LVL 10

Author Comment

by:wildzero
ID: 16925468
Hi there,

Because I am downloading a file from the internet
need to put the memory buffer into a local file on each cycle

so

downlading file
 when each memory buffer is full append to file

that way if the program/computer crashed then it should still resume later without a corrupt file because if I download 1 mb file but kill the prorgram before it's complete the local file still says 1mb when only really say 200k is downloaded...

let me try ur examples
:D
0
 
LVL 10

Author Comment

by:wildzero
ID: 16925637
excellent - thank you :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question