How to add text to a file without erasing its previous contents

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

I already tried to change the command Rewriteto 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;
+4
source share
2 answers
Uses IOUtils;

...

TFile.AppendAllText(filename, sometext);

If you are not working with a truly ancient version of Delphi. http://docwiki.embarcadero.com/VCL/XE/en/IOUtils.TFile.AppendAllText

It also allows you to specify the encoding as a parameter

+9
source

Call Appendto go to the end of the file:

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

, . , : http://docwiki.embarcadero.com/CodeExamples/en/SystemAppend_ (Delphi)

+8

Source: https://habr.com/ru/post/1544451/


All Articles