
[英]Writing datas to memobox from .txt file using Lazarus freepascal?
[英]lazarus / delphi (pascal) - writing to txt file without overwriting next time
我正在使用lazarus,我想保存到txt文件,程序关闭时的信息。 但每次重新打开程序时,它都会覆盖txt文件中之前的内容。 可以写入下一行,甚至每次都创建一个不同的txt文件。 这就是我所拥有的:
var
...
s: TStringList;
s:= TStringList.Create;
s.Add(datetostr(now));
s.SaveToFile(datetostr(now)+'.txt');
s.Free;
但是我给了我一个错误
您需要做的就是以追加模式打开文件,然后添加文本。 它会将新数据放在文件的末尾:
AssignFile(tfOut, C_FNAME);
try
// Open for append, write and close.
append(tfOut);
writeln(tfOut, 'New data for text file');
writeln(tfOut, 'New informtion should be at the end of the file.');
CloseFile(tfOut);
except
on E: EInOutError do
writeln('File error. Elaboration: ', E.Message);
end;
使用tstringlist的另一种方法是:
s:TStringList;
s:= TStringList.Create;
s.loadfromfile('myfile.name');
s.Add(datetostr(now));
(或s.text:= s.text + lineending + datetostr(now))
s.SaveToFile('myfile.name');
s.Free;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.