PB读写文件

文章目录

  • 读写文本
  • 读写二进制文件
  • 源代码

读写文本

1.读取文本
function readtext :读文本内容
function readlines :读文本各行
PB读写文件_第1张图片
2.写入文本
function writetext :覆盖写入文本
function appendtext :追加写入文本
PB读写文件_第2张图片
执行到writetext的文件:
PB读写文件_第3张图片
执行到appendtext的文件:PB读写文件_第4张图片

读写二进制文件

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

源代码

代码拷贝到文本编辑器,另存为 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


你可能感兴趣的:(PB,读写文件,PowerBuilder)