帮忙纠错200(200分)

  • 主题发起人 主题发起人 realism
  • 开始时间 开始时间
R

realism

Unregistered / Unconfirmed
GUEST, unregistred user!
Button1Click保存的文件在读的时候出错,哪位大侠能看出所在?
//窗体上加2个Button和5个Edit即可

procedure TForm1.Button1Click(Sender: TObject);
var
Stream : TFileStream;
S : String;
procedure SaveStr(var S : String);
var
Size : Integer;
begin
Size := Length(S);
Stream.Write(Size,4);
Stream.Write(S,Size);
end;
begin
Stream := TFileStream.Create('D:/ddd.txt',fmCreate or fmOpenWrite);
S := 'AAA'
SaveStr(S);
S := 'BB'
SaveStr(S);
S := 'CSA'
SaveStr(S);
S := 'DDDDD'
SaveStr(S);
S := 'EEEEE'
SaveStr(S);
Stream.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
Stream : TFileStream;
S : String;
Function LoadStr : String;
var
Size : Cardinal;
P : PChar;
begin
P := nil;
Stream.Read(Size,4);
if Size>0 then
begin
P := StrAlloc(Size+1);
Stream.ReadBuffer(P,Size);
P[Size] := #0;
end;
result := P;
end;
begin
Stream := TFileStream.Create('D:/ddd.txt',fmOpenRead);
S := LoadStr
Edit1.Text := s;
S := LoadStr
Edit2.Text := s;
S := LoadStr
Edit3.Text := s;
S := LoadStr
Edit4.Text := s;
S := LoadStr
Edit5.Text := s;
Stream.Free;
end;
 
各位,程序很简单嘛,是没人会,还不愿理会呀,总得帮帮忙吧。
 
为什么???!!!
 
将loadstr改为下面的,
Function LoadStr : String;
var
Size : Cardinal;
P : string;
begin
Stream.Read(Size,4);
if Size>0 then
begin
Stream.ReadBuffer(P,Size);
end;
result := P;
end;
当savestr时只要length(s)>2程序执行就没问题,但<=2时就不行, 我也很奇怪???
 
接受答案了.
 
后退
顶部