How to save a string to a .txt file in Delphi?

75,743

Solution 1

The modern way is to create a stringlist and save that to file.

procedure MakeAStringlistAndSaveThat;
var
  MyText: TStringlist;
begin
  MyText:= TStringlist.create;
  try
    MyText.Add('line 1');
    MyText.Add('line 2');
    MyText.SaveToFile('c:\folder\filename.txt');
  finally
    MyText.Free
  end; {try}
end;

Note that Delphi already has a related class that does everything you want: TInifile.
It stores values and keys in a key = 'value' format.

passwordlist:= TInifile.Create;
try
  passwordlist.LoadFromFile('c:\folder\passwords.txt');
  //Add or replace a password for `user1`
  passwordlist.WriteString('sectionname','user1','topsecretpassword');
  passwordlist.SaveToFile('c:\folder\passwords.txt');
finally
  passwordlist.Free;
end; {try}

Warning
Note that saving unecrypted passwords in a textfile is a security-leak. It's better to hash your passwords using a hashfunction, see: Password encryption in Delphi
For tips on how to save passwords in a secure way.

Solution 2

The Modern Modern way is to use TFile.WriteAllText in IOUtils (Delphi 2010 and up)

procedure WriteAllText(const Path: string; const Contents: string); overload; static;

Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.

Solution 3

You can use the TFileStream class to save a string to a file:

uses
  Classes;

procedure StrToFile(const FileName, SourceString : string);
var
  Stream : TFileStream;
begin
  Stream:= TFileStream.Create(FileName, fmCreate);
  try
    Stream.WriteBuffer(Pointer(SourceString)^, Length(SourceString));
  finally
    Stream.Free;
  end;
end;

and to read

function FileToStr(const FileName : string):string;
var
  Stream : TFileStream;
begin
  Stream:= TFileStream.Create(FileName, fmOpenRead);
  try
    SetLength(Result, Stream.Size);
    Stream.Position:=0;
    Stream.ReadBuffer(Pointer(Result)^, Stream.Size);
  finally
    Stream.Free;
  end;
end;

Solution 4

Fastest and simplest way, no need to declare any variables:

with TStringList.Create do
 try
  Add(SomeString);
  SaveToFile('c:\1.txt');
 finally
  Free;
 end;
Share:
75,743
Nyt Ryda
Author by

Nyt Ryda

Updated on January 04, 2020

Comments

  • Nyt Ryda
    Nyt Ryda over 4 years

    I need to make a program that generates a password that is saved in a text file format in a specific destination I set and the user needs to open the .txt to get the password to 'unlock' another program.

    I have already got the code to generate the password in the string sPass and now I need to use the SaveToFile function to save it into the text file I created called Password.txt but I cannot find the general form to use the SaveTo File Function in Delphi and I do not know where to put the sPass and Password.txt in the function.

    It should be something like : SaveToFile(...) but I do not know how to save sPass in Password.txt

    Edit :

    Just one more question, how do you delete what is previously stored in Password.txt before you add the string to it so that Password.txt is blank before the string is added ? Thanks