joel_kruger
joel_kruger

Reputation: 135

How would I create a text file in Delphi

I have a program where users register for my program and a text file is created for them. I have tried using the CreateFile function but am not sure on the parameters. How would I create a textfile for each user as they register using this, or any other function?

Upvotes: 3

Views: 19068

Answers (4)

Riley
Riley

Reputation: 1

Another Much Simpler Way To Create a File (For those who found this page years later like I did) is to simply use AssignFile('FileName.TXT'). It creates a fresh file that you can upload text to.

Example(My Gr10 Highschool Delphi Practical):

begin
  AssignFile(TFReceipt,'Receipt.TXT');
  redtEReceiptBod.Lines.SaveToFile('Receipt.TXT');
  Showmessage('Receipt generated');
end;

It Takes the content of any string and places it in the file. To make the file readable (Remove the page break markers like /par) you can change the property of the Rich Edit (or any other string container) to RichEdit.plaintext:=true; This removes all the unnecessary page break content.

Warning Keep in mind this was from my Highschool project so it is not very complex and might not work in all circumstances.

Upvotes: 0

Wouter van Nifterick
Wouter van Nifterick

Reputation: 24086

There are several ways to write two lines to a text file:

Using TFile:

fn := 'out.txt';

// method 1
TFile.WriteAllLines(fn, ['Line1','Line2']);

// method 2
TFile.WriteAllText(fn, 'Line1'+sLineBreak+'Line2');

// method 3
TFile.WriteAllText(fn, 'Line1');
TFile.AppendAllText(fn, sLineBreak);
TFile.AppendAllText(fn, 'Line2');

Using TStringList:

const fn := 'out.txt';
var sl := TStringList.Create;
try
  sl.Add('Line1');
  sl.Add('Line2');
  sl.SaveToFile(fn);
finally
  sl.Free;
end;

Upvotes: 6

stevenvh
stevenvh

Reputation: 3139

Shortest answer possible:

FileCreate('filename.txt');

FileCreate is declared in System.SysUtils and is actually a function returning a THandle. (The return value is simply discarded when you don't assign it upon calling.) I'm just showing the above code for snappiness; most of the time it's not good programming practice to ignore the return value. Especially in this case where you definitely will need it further on.

The return value is INVALID_HANDLE_VALUE if the call was not successful, otherwise it holds the file handle of the newly created file. Even if you don't need to use the file right away, you'll need the handle: the file will not be accessible until the handle is released by calling the FileClose function. So, the proper shortest answer becomes

FileClose(FileCreate('filename.txt');

Upvotes: 4

Andreas
Andreas

Reputation: 1364

Maybe you can 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;

Upvotes: 7

Related Questions