为什么不能够在次写入文件,关于FileStream.(50分)

  • 主题发起人 主题发起人 wl_5545831
  • 开始时间 开始时间
W

wl_5545831

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm_Main.Button2Click(Sender: TObject);
var
buf:array[1..6] of byte;
tof:TFileStream;
Year,Month,Day,Hour,Min,Sec,Msec:word;
begin
DecodeDate(now,Year,Month,Day);
DecodeTime(now,Hour,Min,Sec,MSec);
Year:=Year-2000;
buf[1]:=Year;
buf[2]:=Month;
buf[3]:=Day;
buf[4]:=Hour;
buf[5]:=Min;
buf[6]:=Sec;
tof:=TFileStream.Create('test.dat',fmCreate);
tof.Position:=0;
//tof.Seek(tof.Size,0);
tof.WriteBuffer(buf,6);
tof.Free;
end;

当我点击按钮后,文件的大小只有6个字节。但是再次按下后文件没有变化。请问如何连续向文件添加记录?
 
var
buf:array[1..6] of byte;
tof:TFileStream;
Year,Month,Day,Hour,Min,Sec,Msec:word;
FileName:string;
Mode:Integer;
begin
DecodeDate(now,Year,Month,Day);
DecodeTime(now,Hour,Min,Sec,MSec);
Year:=Year-2000;
buf[1]:=Year;
buf[2]:=Month;
buf[3]:=Day;
buf[4]:=Hour;
buf[5]:=Min;
buf[6]:=Sec;
FileName:='c:/test.dat';
if FileExists(FileName) then
Mode:=fmOpenReadWrite
else
Mode:=fmCreate;
tof:=TFileStream.Create(FileName,Mode);
try
tof.Seek(0,soEnd);
tof.WriteBuffer(buf,6);
finally
tof.Free;
end;
end;
 
后退
顶部