Link to home
Create AccountLog in
Editors IDEs

Editors IDEs

--

Questions

--

Followers

Top Experts

Avatar of doubledel
doubledel

Append TStringList to text file in Delphi
Hi experts,
is there a way in Delphi of appending a TStringList to a text file. SaveToFile only overwrites the file but doesnt append to the end of the file.
Thank you

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of Tyler LaczkoTyler Laczko🇨🇦

Example code : Appending a line of text to an existing file
var
  myFile : TextFile;
  text   : string;

begin
  // Try to open the Test.txt file for writing to
  AssignFile(myFile, 'Test.txt');
  ReWrite(myFile);

  // Write a couple of well known words to this file
  WriteLn(myFile, 'Hello');
  WriteLn(myFile, 'World');

  // Close the file
  CloseFile(myFile);

  // Reopen to append a final line to the file
  Append(myFile);

  // Write this final line
  WriteLn(myFile, 'Final line added');

  // Close the file
  CloseFile(myFile);

  // Reopen the file for reading
  Reset(myFile);

  // Display the file contents
  while not Eof(myFile) do
  begin
    ReadLn(myFile, text);
    ShowMessage(text);
  end;

  // Close the file for the last time
  CloseFile(myFile);
end;

ASKER CERTIFIED SOLUTION
Avatar of 22661802266180🇺🇸

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of SteveBaySteveBay🇺🇸

Another way:

var sl : TStringList;
Begin
    sl := TStringList.Create;
    if FileExists(FileName) then
        sl.LoadFromFile(FileName);
    sl.AddStrings(OtherStringList);
    sl.SaveToFile(FileName);
    sl.free;
end;
 
   

Open in new window


Avatar of 22661802266180🇺🇸

it's not too optimal :) consider the case when you want to just add lines. like a logging system. or any other case in which you don't care what;s in the file, you jsut need to add content. if the file grows to 100 MB, your solution will be very slow and will eat up memory, whereas a simple append will not care about what is in the file and will have the same speed and memory consumption over execution ;)
plus you don't provide resource protection. if file exists but you have no rights to read it or write to it, you will hvae an error on that line which will have as a side effect a memory leak :)
otehr than that it's an interesting solution.

this post is only meant as educational :)

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.

Editors IDEs

Editors IDEs

--

Questions

--

Followers

Top Experts

Development in most programming languages is frequently done with an editor or integrated development environment (IDE). An IDE is a software application that provide comprehensive facilities to computer programmers for software development. An IDE normally consists of a source code editor, build automation tools and a debugger. XCode, Visual Studio, Adobe Dreamweaver and Eclipse are some of the more popular development tools, but an editor or IDE can be anything from simple text editors to sophisticated programs. Related topics: All programming and development language, database and web based content management systems topics