Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to append text to a file without erasing its previous content

Tags:

delphi

How do I make Delphi write to a text file without erasing the file's previous contents? I already know how to add text but as soon as I try adding more it just replaces the previous text that was already in the file.

I have already tried changing the Rewrite command to Write.

procedure TForm1.BtnokClick(Sender: TObject); 
var 
    myfile :textfile;
    naam, van, adress : string;
begin 
     adress := edtadress.Text;
     van:= edtvan.Text;
     naam := edtnaam.Text; 
     AssignFile(myfile,'C:\test.txt');
     write(myfile);
     Writeln(myfile,naam);
     writeln(myfile,van);
     writeln(myfile,adress);
     closefile(myfile);
end;
like image 893
user3737296 Avatar asked Jun 13 '14 10:06

user3737296


2 Answers

Uses IOUtils;

...

TFile.AppendAllText(filename, sometext);

Unless you're working with a really ancient Delphi version. http://docwiki.embarcadero.com/VCL/XE/en/IOUtils.TFile.AppendAllText

It also lets you specify an encoding as a parameter

like image 68
Wouter van Nifterick Avatar answered Nov 06 '22 17:11

Wouter van Nifterick


Call Append to move to the end of the file:

AssignFile(myfile, filename);
Append(myfile);
Write(myfile, sometext);
....

Please refer to the documentation. In particular this code example: http://docwiki.embarcadero.com/CodeExamples/en/SystemAppend_(Delphi)

like image 45
David Heffernan Avatar answered Nov 06 '22 15:11

David Heffernan