■■问个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"确覆盖了文件第一行,是怎么回事?
请教
...全文
114 点赞 收藏 14
写回复
14 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
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")
回复
相关推荐
发帖
PowerBuilder
创建于2007-09-28

866

社区成员

PowerBuilder 相关问题讨论
申请成为版主
帖子事件
创建了帖子
2003-05-10 05:04
社区公告
暂无公告