How would I create a text file in Delphi

与世无争的帅哥 提交于 2019-12-11 10:06:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!