Solved

file writing without overwriting

Posted on 2002-04-23
8
172 Views
Last Modified: 2010-04-04

How can i insert text from multiple files in one file? The file where i would want to write to doesnt have an extension.
My problem is that I want to insert data in a file that already contains data. when i do something like this:

try
    // source file
    assignfile(sourcefile,'C:\Documents and     Settings\Administrator\Desktop\source');
    reset(sourcefile);
    // destination file
    Rewrite(destination,'c:\destination);
    append(destination);
    pointer := eof(destination);
    while not eof(source') do
    begin
      ReadLn( source', line);
      writeln(destination,line);
    end;
  finally
    CloseFile(source');
    CloseFile(destination);
  end;

when i run this multiple times, it overwrites data in the destination file. that sux.
0
Comment
Question by:peterjoustra
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 6

Expert Comment

by:DrDelphi
ID: 6962642
Load the file into a TstringList and the new lines with the Add method. Then save to file. Viola!
 Behold:

var list:TstringList;
begin
     list:=Tstringlist.create;
     List.LoadFromFile('Z:\myfile');
     List.add('this is a new line '+TimeTostr(now));
     List.add('So is this one '+TimeTostr(now));
     List.SaveToFile('Z:\myfile');
     FreeAndNil(list);
end;



Good luck!!

 
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6962645
>Rewrite(destination,'c:\destination);
allways creates a new file, do a check for existence before using rewrite , if the file not exists, otherwise use reset

look at fileexists-function

meikl ;-)
   
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6962647
or do as drdelphi suggested
0
 
LVL 17

Accepted Solution

by:
geobul earned 200 total points
ID: 6962699
Hi,

procedure AppendSecondFile(Source, Dest: string);
var
 NewFile: TFileStream;
 OldFile: TFileStream;
begin
 NewFile:=TFileStream.Create(Dest, fmOpenReadWrite);
 NewFile.Seek(0,soFromEnd);
 OldFile:=TFileStream.Create(Source, fmShareDenyNone);
 NewFile.CopyFrom(OldFile, OldFile.Size);
 FreeAndNil(OldFile);
 FreeAndNil(NewFile);
end;

Usage:
// add 'c:\test2' to 'c:\temp\test1'
procedure TForm1.Button1Click(Sender: TObject);
begin
  AppendSecondFile('c:\test2','c:\temp\test1');
end;

Regards, Geo
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 17

Expert Comment

by:geobul
ID: 7169115
Why does this question have 'Pending delete' status?
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9004030
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept answer from DrDelphi

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer
0
 
LVL 17

Expert Comment

by:geobul
ID: 9004286
Well, I have to disagree again. DrDelphi's solution doesn't add one file at the end of another. It could be modified to do so, of course.

Regards, Geo
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9004893
Objection considered.

I will leave a recommendation in the Cleanup topic area that this question is:

Accept answer from geobul

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Paul (pnh73)
EE Cleanup Volunteer
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

19 Experts available now in Live!

Get 1:1 Help Now