有一段程序:如下
h:=CreateFile(pCHAR('C:\1.txt'),GENERIC_READ,0,Nil,OPEN_EXISTING,0,0);//打开文件
if h<>INVALID_HANDLE_VALUE THEN
BEGIN
IF ReadFile(h,strs,120,w,NIL) THEN//读文件
IF STRS<>NIL THEN
edit1.Text:=StrPas(strs);//报错 access violation
用watch跟踪,ReadFile之后,strs没有内容,但w确实是读取的字节数。
在线等待.
h:=CreateFile(pCHAR('C:\1.txt'),GENERIC_READ,0,Nil,OPEN_EXISTING,0,0);//打开文件
if h<>INVALID_HANDLE_VALUE THEN
BEGIN
IF ReadFile(h,strs,120,w,NIL) THEN//读文件
IF STRS<>NIL THEN
edit1.Text:=StrPas(strs);//报错 access violation
用watch跟踪,ReadFile之后,strs没有内容,但w确实是读取的字节数。
在线等待.
readfile不能成功返回。
SetLength(strs, 120);不可以,编译通不过。
strs: Pchar;
begin
....
strs:= Pchar(AllocMem(120));
if ReadFile(h,strs,120,w,nil) then
.....end;try it ?
strs : string;setLength(strs,120);
if ReadFile(h,Pchar(strs),120,w,nil) then
.....
edit1.Text:=StrPas(String(PChar(strs)));//
你们的方法,我都试过了,不行。现在我知道
问题不是内存的分配,而是readfile,因为
ReadFile(h,strs,120,w,NIL) 后,strs中个根本没有内容。
strs:Pchar;
GetMem(strs,120);
ReadFile(h,strs^,120,w,NIL);
其实,sysu(死树) 是正确的,只不过,两处不连续的回复,我没有明白。