Solved

file writing without overwriting

Posted on 2002-04-23
8
176 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
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
 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

789 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