PB读写文件

PB读写文件文章目录读写文本读写二进制文件源代码读写文本1.读取文本functionreadtext:读文本内容functionreadlines:读文本各行2.写入文本functionwritetext:覆盖写入文本functionappendtext:追加写入文本执行到writetext的文件:执行到appendtext的文件:读写二进制文件functionr…

大家好,欢迎来到IT知识分享网。

读写文本

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


免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/23763.html

(0)

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信