檔案流不能讀到動態數組?.

來源:互聯網
上載者:User
檔案流不能讀到動態數組?. VCL組件開發及應用
http://www.delphi2007.net/DelphiVCL/html/delphi_20061226161917101.html
為什麼檔案流不能讀到動態數組?   代碼以下  
  procedure   tform1.ComeBackData();  
          var   filespath:string;  
              tFile:TFileStream;  
              tsize,i:longint;  
              tempArr:array   of   byte;  
   
          begin  
                    if   openDialog1.Execute   then  
                        filespath:=openDialog1.FileName   ;   //取得開啟的檔案名稱  
                    if   filespath=''   then  
                        exit;  
                    tFile:=TFileStream.Create(filespath,fmOpenRead);       //   建立二進位檔案  
                    tsize:=tfile.Size;   //取得總的長度  
   
                    SetLength(tempArr,tsize);     //計算動態數組的長度  
                    //分配動態數組的空間  
                    GetMem(temparr,   tsize   *   SizeOf(byte));  
   
                  tfile.Read(temparr,tsize);  
   
                    FreeMem(temparr,   tsize*SizeOf(byte));  
          end;  
   
  以上代碼檔案流不能讀到動態數組,但可以讀到靜態數組裡,請問為什麼?

procedure   tform1.ComeBackData();  
          var   filespath:string;  
              tFile:TFileStream;  
              tsize,i:longint;  
              tempArr:array   of   byte;  
   
          begin  
                    if   openDialog1.Execute   then  
                        filespath:=openDialog1.FileName   ;   //取得開啟的檔案名稱  
                    if   filespath=''   then  
                        exit;  
                    tFile:=TFileStream.Create(filespath,fmOpenRead);       //   建立二進位檔案  
                    tsize:=tfile.Size;   //取得總的長度  
   
                    SetLength(tempArr,tsize);     //設定(而不是計算)動態數組的長度  
                    tfile.Read(tempArr[0],tsize);  
          end;

//分配動態數組的空間  
                    GetMem(temparr,   tsize   *   SizeOf(byte));  
   
  這個不需要的

procedure   tform1.ComeBackData();  
          var   filespath:string;  
              tFile:TFileStream;  
              tsize,i:longint;  
              tempArr:array   of   byte;  
   
          begin  
                    if   openDialog1.Execute   then  
                        filespath:=openDialog1.FileName   ;   //取得開啟的檔案名稱  
                    if   filespath=''   then  
                        exit;  
                    tFile:=TFileStream.Create(filespath,fmOpenRead);       //   建立二進位檔案  
                    tsize:=tfile.Size;   //取得總的長度  
   
                    SetLength(tempArr,tsize);     //計算動態數組的長度  
                    //分配動態數組的空間  
                    //GetMem(temparr,   tsize   *   SizeOf(byte));  
                    tFile.Position:=0;  
                  tfile.Read(temparr[0],tsize);  
   
                  //   FreeMem(temparr,   tsize*SizeOf(byte));  
          end;  
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.