我制作了一个简单的程序,将一些信息(姓名,姓名,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
文件,而是重写它。我知道我正在做某事,如果有人不介意帮我一把。
由于
答案 0 :(得分:4)
在添加文件并将其写回之前,在开头加载文件(通过LoadFromFile
);或者忘记TStringList
,并在使用WriteLn
打开文件后使用Append
。
答案 1 :(得分:1)
procedure TForm1.BitBtn1Click(Sender: TObject);
var
InfoFile : TStringList;
Name, Surname, ExtraInfo : String;
PhoneNumber,ID : Integer;
Date : TDateTime;
FS : TFileStream;
begin
Name := edtName.text;
Surname := edtSurname.Text;
ID := StrToInt64(edtID.Text);
PhoneNumber := StrToInt64(edtPhone.Text);
Date := StrToDate(edtJoinDate.Text);
InfoFile := TStringList.Create;
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('');
FS := TFileStream.Create('C:\Users\GrassMan\Desktop\InfoSaver\imfofile.txt', fmOpenWrite);
try
FS.Seek(0, soEnd);
InfoFile.SaveToStream(FS);
finally
FS.Free;
end;
finally
InfoFile.Free;
end;
end;
答案 2 :(得分:1)
应该是这样的:
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;
答案 3 :(得分:0)
你应该使用TFileStream:
var
recordStr: string;
fs: TFileStream;
fsFlags: Word;
filePath: string;
begin
filePath := 'C:\Users\GrassMan\Desktop\InfoSaver\imfofile.txt';
recordStr := 'NAME: '+ Name + #13#10 +
'SURNAME: '+ Surname + #13#10 +
'ID NUMBER: '+ IntToStr(ID) + #13#10 +
'PHONE NUMBER: '+ IntToStr(PhoneNumber) + #13#10 +
'DATE JOINED :' + DateToStr(Date) + #13#10 +
#13#10#13#10; // Spaces to separate next set of details
// open if exists, create if not
fsFlags := fmOpenWrite;
if (not FileExists(filePath)) then
fsFlags := fsFlags or fmCreate;
try
fs := TFileStream.Create(filePath);
try
fs.Seek(0, soEnd); // go to the end of the file
fs.Write(recordStr[1], Length(recordStr));
finally
fs.Free;
end;
except on ex: Exception do
begin
ShowMessage('Error while writing to the file: ' + ex.Message);
end;
end;
end;