有一段程序:如下   
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确实是读取的字节数。
在线等待.

解决方案 »

  1.   

    IF ReadFile(h,strs^,120,w,NIL) THEN
      

  2.   

    IF ReadFile(h,strs^,120,w,NIL) THEN 
    readfile不能成功返回。
      

  3.   

    strs有没有分配空间?比如 SetLength(strs, 120);
      

  4.   

    我用stralloc分配过,也不行。
    SetLength(strs, 120);不可以,编译通不过。
      

  5.   

    var
      strs: Pchar;
    begin
      ....
      strs:= Pchar(AllocMem(120));
      if ReadFile(h,strs,120,w,nil) then
        .....end;try it ?
      

  6.   

    var
      strs : string;setLength(strs,120);
    if ReadFile(h,Pchar(strs),120,w,nil) then
        .....
    edit1.Text:=StrPas(String(PChar(strs)));//
      

  7.   

    JackyChang(摩托骡拉) , hotzhu(非洲白脸) :
    你们的方法,我都试过了,不行。现在我知道
    问题不是内存的分配,而是readfile,因为
    ReadFile(h,strs,120,w,NIL) 后,strs中个根本没有内容。
      

  8.   

    有人告诉我这样做
    strs:Pchar;
    GetMem(strs,120);
    ReadFile(h,strs^,120,w,NIL);
    其实,sysu(死树) 是正确的,只不过,两处不连续的回复,我没有明白。