PB读写文件

读写文本

1.读取文本
function readtext :读文本内容
function readlines :读文本各行
在这里插入图片描述
2.写入文本
function writetext :覆盖写入文本
function appendtext :追加写入文本
在这里插入图片描述
执行到writetext的文件:
在这里插入图片描述
执行到appendtext的文件:在这里插入图片描述

读写二进制文件

function readfile 读取文件
function writefile 覆盖写入文件
function appendfile 追加写入文件
在这里插入图片描述
在这里插入图片描述
写入的新文件SQLMonitor_write.rar与原文件完全一致,可以正常打开
在这里插入图片描述
在这里插入图片描述

源代码

代码拷贝到文本编辑器,另存为 n_func_file.sru,导入pbl
发现BUG请留言或私信,以便修正(QQ:768310524 TEL:18649713925)

$PBExportHeader$n_func_file.sru
forward
global type n_func_file from nonvisualobject
end type
end forward

global type n_func_file from nonvisualobject autoinstantiate
end type

forward prototypes
public function integer readfile (string as_file, ref blob rblob_file)
public function integer readtext (string as_file, ref string rs_text)
public function integer readlines (string as_file, ref string rs_line[])
public function integer writefile (string as_file, blob ablob_file)
public function integer writetext (string as_file, string as_text)
public function integer appendtext (string as_file, string as_text)
public function integer appendfile (string as_file, blob ablob_file)
end prototypes

public function integer readfile (string as_file, ref blob rblob_file);long ll_len
long ll_loop,i
int li_filenum,li_read
blob lblob_read,lblob_file

ll_len = filelength(as_file)
ll_loop = ll_len / 32765
if mod(ll_len,32765) <> 0 then ll_loop += 1

li_filenum = fileopen(as_file,streammode!,read!,lockread!)
if li_filenum < 0 then return li_filenum

for i = 1 to ll_loop
	li_read = fileread(li_filenum,lblob_read)
	if li_read < 0 then return li_read
	lblob_file += lblob_read
next
fileclose(li_filenum)

rblob_file = lblob_file
return 0
end function

public function integer readtext (string as_file, ref string rs_text);long ll_len
long ll_loop,i
int li_filenum,li_read
string ls_read,ls_file

ll_len = filelength(as_file)
ll_loop = ll_len / 32765
if mod(ll_len,32765) <> 0 then ll_loop += 1

li_filenum = fileopen(as_file,streammode!,read!,lockread!)
if li_filenum < 0 then return li_filenum

for i = 1 to ll_loop
	li_read = fileread(li_filenum,ls_read)
	if li_read < 0 then return li_read
	ls_file += ls_read
next
fileclose(li_filenum)

rs_text = ls_file
return 0
end function

public function integer readlines (string as_file, ref string rs_line[]);int li_filenum,li_read
string ls_read,ls_line[]
long i

li_filenum = fileopen(as_file,LineMode!,Read!,LockRead!)
if li_filenum < 0 then return li_filenum

do while fileread(li_filenum,ls_read) >= 0
	i += 1
	ls_line[i] = ls_read
loop

fileclose(li_filenum)

rs_line = ls_line
return 0
end function

public function integer writefile (string as_file, blob ablob_file);long ll_len
long ll_loop,i
int li_filenum,li_write
blob lblob_write

ll_len = len(ablob_file)
ll_loop = ll_len / 32765
if mod(ll_len,32765) <> 0 then ll_loop += 1

li_filenum = fileopen(as_file,StreamMode!,Write!,LockWrite!,Replace!)
if li_filenum < 0 then return li_filenum

for i = 1 to ll_loop
	lblob_write = blobmid(ablob_file,(i - 1)*32765 + 1,32765)
	li_write = filewrite(li_filenum,lblob_write)
	if li_write < 0 then return li_write
next

fileclose(li_filenum)
return 0
end function

public function integer writetext (string as_file, string as_text);long ll_len
long ll_loop,i
int li_filenum,li_write
string ls_write

ll_len = len(as_text)
ll_loop = ll_len / 16382
if mod(ll_len,16382) <> 0 then ll_loop += 1

li_filenum = fileopen(as_file,StreamMode!,Write!,LockWrite!,Replace!)
if li_filenum < 0 then return li_filenum

for i = 1 to ll_loop
	ls_write = mid(as_text,(i - 1)*16382 + 1,16382)
	li_write = filewrite(li_filenum,ls_write)
	if li_write < 0 then return li_write
next

fileclose(li_filenum)
return 0
end function

public function integer appendtext (string as_file, string as_text);long ll_len
long ll_loop,i
int li_filenum,li_write
string ls_write

ll_len = len(as_text)
ll_loop = ll_len / 16382
if mod(ll_len,16382) <> 0 then ll_loop += 1

li_filenum = fileopen(as_file,StreamMode!,Write!,LockWrite!,Append!)
if li_filenum < 0 then return li_filenum

for i = 1 to ll_loop
	ls_write = mid(as_text,(i - 1)*16382 + 1,16382)
	li_write = filewrite(li_filenum,ls_write)
	if li_write < 0 then return li_write
next

fileclose(li_filenum)
return 0
end function

public function integer appendfile (string as_file, blob ablob_file);long ll_len
long ll_loop,i
int li_filenum,li_write
blob lblob_write

ll_len = len(ablob_file)
ll_loop = ll_len / 32765
if mod(ll_len,32765) <> 0 then ll_loop += 1

li_filenum = fileopen(as_file,StreamMode!,Write!,LockWrite!,Append!)
if li_filenum < 0 then return li_filenum

for i = 1 to ll_loop
	lblob_write = blobmid(ablob_file,(i - 1)*32765 + 1,32765)
	li_write = filewrite(li_filenum,lblob_write)
	if li_write < 0 then return li_write
next

fileclose(li_filenum)
return 0
end function

on n_func_file.create
call super::create
TriggerEvent( this, "constructor" )
end on

on n_func_file.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on


  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值