谢谢jijl2001,给我如此详细的答复
我在别人的原代码中看到这样一段代码:
var
FileRec:TStringList;
FileName:string;
begin
FileRec:=TStringList.Create;
try
FileName:=ExtractFilePath(Application.ExeName)+FormatDateTime('yyyymmdd',Now)+'.txt';
if FileExists(FileName) then
FileRec.LoadFromFile(FileName);
end
这段代码的效果是把数据保存到自动生成一个用日期为标题的文本文件中,
它这里只用了FileRec.LoadFromFile(FileName)就把数据载入到文本里了,能帮我看看吗?
procedure TLogFileForm.FileNewItemClick(Sender: TObject);
var
Txt: TextFile;
Line,DataFile: string;
i:integer;
begin
if SaveDialog1.Execute then begin
DataFile:=SaveDialog1.FileName+'.txt';
AssignFile(Txt, DataFile);
try
ReWrite(Txt);
Line := SetSpace(Listview1.Columns.Items[0].Caption,20) +
SetSpace(Listview1.Columns.Items[1].Caption,20) +
SetSpace(Listview1.Columns.Items[2].Caption,10) +
SetSpace(Listview1.Columns.Items[3].Caption,20) +
Listview1.Columns.Items[4].Caption ;
Writeln(Txt, Line);
For i:=0 to Listview1.Items.Count-1 do begin
Line :=SetSpace(Listview1.Items[i].Caption,20) +
SetSpace(Listview1.Items[i].SubItems.Strings[0],20) +
SetSpace(Listview1.Items[i].SubItems.Strings[1],10) +
SetSpace(Listview1.Items[i].SubItems.Strings[2],20) +
Listview1.Items[i].SubItems.Strings[3];
function SetSpace(Txt:string;L:integer):String;
var
i,a:integer;
begin
if Length(Txt)>L then begin
Result:=Copy(txt,1,L);
end else begin
a:=L-Length(txt);
For i:=a to L do begin
txt:=txt+' ';
end;
Result:=txt;
end;
end;
看看吧,