问题
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?
回答1:
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;
回答2:
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
:
fn := 'out.txt';
sl := TStringList.Create;
try
Add('Line1');
Add('Line2');
WriteToFile(fn);
finally
sl.Free;
end;
来源:https://stackoverflow.com/questions/51746448/how-would-i-create-a-text-file-in-delphi