delphi - 在文本文件Delphi中添加而不是替换项目

标签 delphi delphi-7

我编写了一个简单的程序,将一个信息(名称,姓氏,ID等)添加到.txt文件中。每当我在程序中添加新的详细信息并单击按钮以保存信息时,它就会将其重写为.txt文件。

这是我的代码:

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  InfoFile                 : TStringList;
  Name, Surname, ExtraInfo : String;
  PhoneNumber,ID           : Integer;
  Date                     : TDateTime;
begin
  InfoFile := TStringList.Create;
  Name := edtName.text;
  Surname := edtSurname.Text;
  ID := StrToInt64(edtID.Text);
  PhoneNumber := StrToInt64(edtPhone.Text);
  Date := StrToDate(edtJoinDate.Text);

  try
    InfoFile.Add('NAME: '+Name);
    InfoFile.Add('SURNAME: '+Surname);
    InfoFile.Add('ID NUMBER: '+IntToStr(ID));
    InfoFile.Add('PHONE NUMBER: '+IntToStr(PhoneNumber));
    InfoFile.Add('DATE JOINED :'+DateToStr(Date));
    InfoFile.Add('');         // Spacers to separate next set of details
    InfoFile.Add('');

    InfoFile.SaveToFile('C:\Users\GrassMan\Desktop\InfoSaver\imfofile.txt');
  finally
    InfoFile.Free;
  end;


因此,与其重写新的细节到.txt文件,不如重写它。我知道我在做某事,如果有人不介意帮我。

谢谢

最佳答案

应该看起来像这样:

begin
    InfoFile := TStringList.Create;
    Name := edtName.text;
    Surname := edtSurname.Text;
    ID := (edtID.Text);
    PhoneNumber :=(edtPhone.Text);
  try
    InfoFile.LoadFromFile('C:\Users\grassman\Desktop\infofile.txt');
    InfoFile.Add('NAME: '+Name);
    InfoFile.Add('SURNAME: '+Surname);
    InfoFile.Add('ID NUMBER: '+ ID);
    InfoFile.Add('PHONE NUMBER: '+(PhoneNumber));
    InfoFile.Add('Time of registration: ' + TimeToStr(time));
    InfoFile.Add('Date of registration: ' + DateToStr(date));
    InfoFile.Add('');         // Spacers to separate next set of details
    InfoFile.Add('');
    InfoFile.SaveToFile('C:\Users\grassman\Desktop\infofile.txt');
  finally
    InfoFile.Free;

关于delphi - 在文本文件Delphi中添加而不是替换项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22130021/

相关文章:

Delphi7,Showmessage命令在申请表出现之前执行

delphi - 关于工作不正常的 Intraweb 问题(单选按钮)?

delphi - TBitMap 到 PBitMap KOL

delphi - Twitter OAUTH 和 Win32 EXE 桌面应用程序

delphi - TInterfacedObject 自动内存管理也可以与 C++Builder 一起使用吗?

Delphi TClientDataSet 使用 LIKE 和 foCaseInsensitive 进行过滤

delphi - 从 Delphi7 迁移到 Delphi XE 或 2010 for Unicode?

Delphi 使用 Microsoft 的 Shell Doc 对象和控制库 (SHDOCVW.DLL)

delphi - 将多个 TObject 流式传输到 TMemoryStream

delphi - 通过SQL语句和adoquery在delphi中删除数据库