Solved

Append content of TMemoryStream to TFileStream

Posted on 2006-06-16
3
1,314 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
  • 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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 this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now