Solved

Append content of TMemoryStream to TFileStream

Posted on 2006-06-16
3
1,532 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
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 video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month5 days, 10 hours left to enroll

627 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