■■问个PB写文件的问题,大家看看问题出在哪里?■■

yyhyan 2003-05-10 05:04:58
问个PB写文件的问题,我向文件加入一行字符"this is line"
integer li_FileNum
li_FileNum=FileOpen(ls_file,LineMode!,Write!,LockWrite!,Append!)
FileSeek(li_FileNum,0,FromBeginning!)
FileWrite(li_FileNum,"this is line")
我本意是向文件中添加一行"this is line",
没有达到效果,结果"this is line"确覆盖了文件第一行,是怎么回事?
请教
...全文
147 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
888888888888 2003-05-11
  • 打赏
  • 举报
回复
关注
juwuyi 2003-05-11
  • 打赏
  • 举报
回复
可以这样做:
将你现在要写入的数据存到一个临时文件里,然后将临时文件和现有的文件合并起来。
killerdanny 2003-05-11
  • 打赏
  • 举报
回复
//两个函数一个读文件,一个写文件
//还有一个实现添加的调用!

//1.读文件函数!

global type f_readfile from function_object
end type

forward prototypes
global function integer f_readfile (string filename, ref blob fb)
end prototypes

global function integer f_readfile (string filename, ref blob fb);int result,fnum,loops,i;
long flen, bytes_read, new_pos
blob b
/////////////////////////////
fb = b;
// Get the file length, and open the file
flen = FileLength(filename)
fnum = FileOpen(filename,StreamMode!, Read!, Shared!)
IF (fnum <1) THEN
MessageBox("读取文件错误","不能找到或打开此文件["+filename+"]!");
GOTO ERROR;
END IF
// Determine how many times to call FileRead
IF (flen <=0) THEN
MessageBox("读取文件错误","不能打开文件["+filename+"]!",INFORMATION!);
GOTO ERROR;
END IF
IF flen > 32765 THEN
IF Mod(flen, 32765) = 0 THEN
loops = flen/32765
ELSE
loops = (flen/32765) + 1
END IF
ELSE
loops = 1
END IF
////****** Read the file ******//
new_pos = 1
fb = b
FOR i = 1 to loops
bytes_read = FileRead(fnum, b)
fb = fb + b
NEXT
FileClose(fnum);
////////////////////////////
RETURN 1; //***** Return 1 As Successful;
ERROR:
Return -1; //***** Return -1 As Failure



end function

//2.写文件函数!

global type f_writefile from function_object
end type

forward prototypes
global function integer f_writefile (string filename, ref blob fb, boolean bappend)
end prototypes

global function integer f_writefile (string filename, ref blob fb, boolean bappend);int result,fnum,loops,i;
long flen, bytes_read, new_pos
Blob b;
/////////////////////////////
IF ISNULL(b) THEN
MessageBox("错误","no Data need To write to file(blob is null)!",INFORMATION!);
GOTO ERROR;
END IF
// Get the file length, and open the file
flen = len(fb);
// Determine how many times to call FileRead
IF (flen <=0) THEN
// MessageBox("Error","no Data need To write to file(blob=0)!",INFORMATION!);
GOTO ERROR;
END IF
if bAppend then
fnum = FileOpen(filename,StreamMode!, Write!, LockWrite!,Append!);
else
fnum = FileOpen(fileName,StreamMode!,write!,LockWrite!,Replace!);
end if
IF (fnum <1) THEN
MessageBox("打开文件错误","不能打开目标文件~r~n"+fileName);
GOTO ERROR;
END IF
IF flen > 32765 THEN
IF Mod(flen, 32765) = 0 THEN
loops = flen/32765
ELSE
loops = (flen/32765) + 1
END IF
ELSE
loops = 1
END IF
////****** Read the file ******//
new_pos = 1
FOR i = 1 to loops
b = Blobmid(fb,new_pos,32765);
bytes_read = FileWrite(fnum, b)
new_pos +=bytes_read;
NEXT
FileClose(fnum);
////////////////////////////
RETURN 1; //***** Return 1 As Successful;
ERROR:
Return -1; //***** Return -1 As Failure



end function

//

//3.函数调用,在文件头增加一行的代码
blob zw ,zw_1
f_readfile("zw.txt",zw)

integer li_FileNum

li_FileNum = FileOpen("zw.txt", &
LineMode!, Write!, LockWrite!, Replace!)
FileWrite(li_FileNum, "danny hehe")

FileClose(li_FileNum)
f_writefile("zw.txt",zw,true)
freshman113 2003-05-10
  • 打赏
  • 举报
回复
我只是提出方法,并不是完整代码。
当然读写都需通过循环。这在PB帮助都有完整代码。不需要我多说吧!?

This example reads a file exceeding 32,765 bytes. After the script has read the file into the blob tot_b, you can call the SetPicture or String function to make use of the data, depending on the contents of the file:

integer li_FileNum, loops, i

long flen, bytes_read, new_pos

blob b, tot_b

// Set a wait cursor

SetPointer(HourGlass!)

// Get the file length, and open the file

flen = FileLength(sle_filename.Text)

li_FileNum = FileOpen(sle_filename.Text, &

StreamMode!, Read!, LockRead!)

// Determine how many times to call FileRead

IF flen > 32765 THEN

IF Mod(flen, 32765) = 0 THEN

loops = flen/32765

ELSE

loops = (flen/32765) + 1

END IF

ELSE

loops = 1

END IF

// Read the file

new_pos = 1

FOR i = 1 to loops

bytes_read = FileRead(li_FileNum, b)

tot_b = tot_b + b

NEXT

FileClose(li_FileNum)
yyhyan 2003-05-10
  • 打赏
  • 举报
回复
■■freshman113(逍遥子) ■■
//先读后写
integer li_FileNum
li_FileNum=FileOpen(ls_file,StreamMode! ,read!)
fileread(li_FileNum,ls_data)
fileclose (li_FileNum)
ls_data = "this is line~r~n" + ls_data
li_FileNum=FileOpen(ls_file,StreamMode! ,write!,lockwrite!,replace!)
FileWrite(li_FileNum,ls_data)
这样会有读取字节限制得。FileWrite writes a maximum of 32,765 bytes


fileclose(li_FileNum)

tchatcha 2003-05-10
  • 打赏
  • 举报
回复
integer li_FileNum
li_FileNum=FileOpen(ls_file,LineMode!,Write!,LockWrite!,Append!)
//FileSeek(li_FileNum,0,FromBeginning!) //这句去掉
FileWrite(li_FileNum,"this is line")
freshman113 2003-05-10
  • 打赏
  • 举报
回复
//先读后写

integer li_FileNum
li_FileNum=FileOpen(ls_file,StreamMode! ,read!)
fileread(li_FileNum,ls_data)
fileclose (li_FileNum)
ls_data = "this is line~r~n" + ls_data
li_FileNum=FileOpen(ls_file,StreamMode! ,write!,lockwrite!,replace!)
FileWrite(li_FileNum,ls_data)
fileclose(li_FileNum)

yyhyan 2003-05-10
  • 打赏
  • 举报
回复
■■那有什么办法能做到这一点呢?■■
zhouweiwansui 2003-05-10
  • 打赏
  • 举报
回复
不可能写到第一行,这是文件操作方式决定的
yyhyan 2003-05-10
  • 打赏
  • 举报
回复
to ■■freshman113(逍遥子) ■■
那应当怎么写才对呢?
freshman113 2003-05-10
  • 打赏
  • 举报
回复
FileSeek(li_FileNum,0,FromBeginning!) 将pointer指向了文件开始
但FileWrite(li_FileNum,"this is line")是意味着从文件头开始写入(REPLACE)所以,这语句将替换掉len("this is line" + "~r~n")个从文件应开始的字符。
yyhyan 2003-05-10
  • 打赏
  • 举报
回复
■■juwuyi(深白色) :■■
可是我想写入到文件的第一行呀,你看清楚题意
xj1998 2003-05-10
  • 打赏
  • 举报
回复
juwuyi(深白色)说得对!
juwuyi 2003-05-10
  • 打赏
  • 举报
回复
integer li_FileNum
li_FileNum=FileOpen(ls_file,LineMode!,Write!,LockWrite!,Append!)
//FileSeek(li_FileNum,0,FromBeginning!) //这句去掉
FileWrite(li_FileNum,"this is line")

1,108

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder 相关问题讨论
社区管理员
  • 基础类社区
  • WorldMobile
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧