[PB] PB中讀寫檔案通用的兩個函數

來源:互聯網
上載者:User
PB中讀寫檔案通用的兩個函數
1、檔案讀取 :

//函數名:f_readfile
//功能:讀取檔案
//參數:
//  io_file:ref blob 大物件類型,儲存讀出的檔案內容;
//  is_filename:string 檔案名稱
//傳回值:int  1-成功 0-失敗

LONG ll_len
int li_FileNum,li_count,loops,i,bytes_read
blob lblb_b,tot_b
int liResult=1

ll_len=FileLength(is_filename)
IF ll_len > 32765 THEN
 IF Mod(ll_len, 32765) = 0 THEN
  loops = ll_len/32765    
 ELSE
  loops = (ll_len/32765) + 1
 END IF
ELSE
 loops = 1
END IF  

li_FileNum =  FileOpen(is_filename,StreamMode!, read!, lockread!)
if li_filenum = -1 then
 messagebox("","無法開啟檔案"+ is_filename)
end if

FOR i = 1 to loops
 bytes_read=FileRead(li_FileNum, lblb_b)
 if bytes_read = -1 then
        messagebox("","讀取檔案"+is_filename+"失敗!")
    liResult = 0
    exit
 end if
 if i=1 then
  tot_b = lblb_b
 else
  tot_b = tot_b + lblb_b
 end if 
NEXT
Fileclose(li_FileNum)

if liResult = 1 then
 ib_file=tot_b
end if

return liResult

2、檔案寫入 :

//函數名:f_writefile
//功能:寫入檔案
//參數:
//  io_file:blob  大物件類型,儲存將寫入的檔案內容;
//  is_filename:string  檔案名稱
//傳回值:int  1-成功 0-失敗

uLONG ll_len,li_count,loops
int li_FileNum
blob lblb_b
int liResult = 1

ll_len=len(ib_file)
IF ll_len > 32765 THEN
 IF Mod(ll_len, 32765) = 0 THEN
  loops = ll_len/32765    
 ELSE
  loops = (ll_len/32765) + 1
 END IF
ELSE
 loops = 1
END IF  

li_FileNum =  FileOpen(is_filename,StreamMode!, write!, shared!,Replace!)
if li_FileNum = -1 then
 messagebox("系統提示","無法開啟檔案"+is_filename)
 return 0
end if
FOR li_count = 1 to loops
 lblb_b = blobmid( ib_file , ( li_count - 1) * 32765 + 1 , 32765 )   
 if FileWrite(li_FileNum, lblb_b) = -1 then
  messagebox("系統提示","寫檔案" + is_filename+"失敗")
  liResult=0
  exit
 end if//8010
NEXT
Fileclose(li_FileNum)

//if liResult=1 then
// liResult = gf_setfiletime(is_filename,idt_file_time)
//end if

return liResult

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.