Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Append content of TMemoryStream to TFileStream

Posted on 2006-06-16
3
Medium Priority
?
1,635 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 1000 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

971 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