Solved

Append content of TMemoryStream to TFileStream

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Mdi application Child forms get behind control 7 274
find a node in VST 2 73
how to resize animated Gif image in delphi ? 1 35
Graphics32 under Delphi 10.1 Berlin 2 105
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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 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