G
g_liyang
Unregistered / Unconfirmed
GUEST, unregistred user!
各位大虾:
小弟近日编程遇到一怪事,请各位帮忙解释,解释。
Delphi的FileOpen函数的帮助中有以下例子:
procedure TForm1.Button1Click(Sender: TObject);
var
iFileHandle: Integer;
iFileLength: Integer;
iBytesRead: Integer;
Buffers: PChar;
i: Integer;
begin
if OpenDialog1.Execute then
begin
try
iFileHandle := FileOpen(OpenDialog1.FileName, fmOpenRead);
iFileLength := FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
Buffers := StrAlloc(iFileLength + 1);
iBytesRead := FileRead(iFileHandle, Buffers, iFileLength);
FileClose(iFileHandle);
for i := 0 to iBytesRead-1 do
begin
StringGrid1.RowCount := StringGrid1.RowCount + 1;
StringGrid1.Cells[1,i+1] := Buffers;
StringGrid1.Cells[2,i+1] := IntToStr(Integer(Buffers));
end;
finally
FreeMem(Buffers);
end;
end;
end;
我在程序中使用此例子,但总是在 StringGrid1.Cells[1,i+1] := Buffers;
这一句出现EAccessViolation异常。不知是何原因。到底错在何处,还请各位帮忙指出。
小弟近日编程遇到一怪事,请各位帮忙解释,解释。
Delphi的FileOpen函数的帮助中有以下例子:
procedure TForm1.Button1Click(Sender: TObject);
var
iFileHandle: Integer;
iFileLength: Integer;
iBytesRead: Integer;
Buffers: PChar;
i: Integer;
begin
if OpenDialog1.Execute then
begin
try
iFileHandle := FileOpen(OpenDialog1.FileName, fmOpenRead);
iFileLength := FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
Buffers := StrAlloc(iFileLength + 1);
iBytesRead := FileRead(iFileHandle, Buffers, iFileLength);
FileClose(iFileHandle);
for i := 0 to iBytesRead-1 do
begin
StringGrid1.RowCount := StringGrid1.RowCount + 1;
StringGrid1.Cells[1,i+1] := Buffers;
StringGrid1.Cells[2,i+1] := IntToStr(Integer(Buffers));
end;
finally
FreeMem(Buffers);
end;
end;
end;
我在程序中使用此例子,但总是在 StringGrid1.Cells[1,i+1] := Buffers;
这一句出现EAccessViolation异常。不知是何原因。到底错在何处,还请各位帮忙指出。