Link to home
Start Free TrialLog in
Avatar of peterjoustra
peterjoustra

asked on

file writing without overwriting


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.
Avatar of DrDelphi
DrDelphi

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

 
Avatar of kretzschmar
>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 ;-)
   
or do as drdelphi suggested
ASKER CERTIFIED SOLUTION
Avatar of geobul
geobul

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why does this question have 'Pending delete' status?
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
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
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