您的位置:程序门 -> vb -> 基础类



怎样消除vb对文本文件查找替换后在文件尾多出的空行?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


怎样消除vb对文本文件查找替换后在文件尾多出的空行?
发表于:2008-01-19 10:33:19 楼主
下面是对一文本文件进行查找替换的模块:

private   sub   tihuan(tfilepath   as   string,   oldstr   as   string,   newstr   as   string)
dim   k()   as   byte
open   tfilepath   for   binary   as   #1
redim   k(filelen(tfilepath)   -   1)   as   byte
get   #1,   ,   k
k   =   replace(strconv(k,   vbunicode),   oldstr,   newstr)   '无条件全部替换
close   #1

open   tfilepath   for   output   as   #2
print   #2,   k
close   #2
end   sub

'调用方法

private   sub   command2_click()

command2.enabled   =   false

call   tihuan("c:\1.txt",   "旧字符",   "新字符")           ‘     c:\1.txt   可以超过   5   mb以上

command2.enabled   =   true

但在使用中发现,每调用一次,就在文本文件的最后多一个空行   ,   执行1000次就多1000个空行。怎样消除这些空行?

发表于:2008-01-19 12:17:441楼 得分:0
newstr中最后有回车换行,去掉后再替换。
这样试试。
k       =       replace(strconv(k,       vbunicode),       oldstr,       left(newstr,len(newstr)-2))       '无条件全部替换  
发表于:2008-01-19 12:41:212楼 得分:0
你的读写文件的方法很怪,既然只是要进行文本替换,为什么要用二进制读的方式,却又用文本写的方式,
建议你都用文本方式读写,应该可以消除你说的问题,至于为什么,你自己想想吧
发表于:2008-01-19 12:52:113楼 得分:0
谢谢!
用此法:
k               =               replace(strconv(k,               vbunicode),               oldstr,               left(newstr,len(newstr)-2))               '无条件全部替换
试过,无效。

我原来的代码运行后在所查找替换的文本文件的最后部分多了些空行,并不是在查找替换处多了空行。
发表于:2008-01-19 13:34:264楼 得分:0
你的代码写文件后多了2字节,下列代码采用临时文件来去掉该2字节:
vbscript code
option explicit dim tfilepath1 as string dim tfilepath as string private sub tihuan(tfilepath as string, oldstr as string, newstr as string) dim k() as byte dim knew() as byte tfilepath1 = tfilepath open tfilepath for binary as #1 redim k(filelen(tfilepath) - 1) as byte get #1, , k k = replace(strconv(k, vbunicode), oldstr, newstr) '无条件全部替换 close #1 open "c:\tmp.txt" for output as #2 '建立临时文件 print #2, k close #2 fuyuan end sub private sub fuyuan() dim k() as byte dim knew() as byte dim i as long dim l as long kill tfilepath1 open "c:\tmp.txt" for binary as #1 '二进制方式读临时文件 redim k(filelen("c:\tmp.txt") - 1) as byte get #1, , k close #1 l = ubound(k) print ubound(k) redim knew(l - 2) '删除临时文件最后2字节 for i = 0 to l - 2 knew(i) = k(i) next open "c:\1.txt" for binary as #2 '二进制方式写新文件 for i = 0 to ubound(knew) put #2, , k(i) next close #2 kill "c:\tmp.txt" '删除临时文件 end sub '调用方法 private sub command2_click() command2.enabled = false call tihuan("c:\1.txt", "旧字符", "新字符") 'c:\1.txt可以超过5mb以上 command2.enabled = true end sub
发表于:2008-01-19 13:40:515楼 得分:0
fuyuan过程代码简化如下:
vbscript code
private sub fuyuan() dim k() as byte dim i as long dim l as long kill tfilepath1 open "c:\tmp.txt" for binary as #1 '二进制方式读临时文件 redim k(filelen("c:\tmp.txt") - 1) as byte get #1, , k close #1 l = ubound(k) print ubound(k) open "c:\1.txt" for binary as #2 '二进制方式写新文件,最后2字节不写 for i = 0 to l - 2 put #2, , k(i) next close #2 kill "c:\tmp.txt" '删除临时文件 end sub
发表于:2008-01-19 13:42:026楼 得分:0
如都用文本方式读写,连替换都不能进行。
我估计是print输出造成的,将输出部分改写为
open   tfilepath   for   binary   as   #2
put   #2,   ,   trim(k)
close   #2
可消除文本文件尾部的空行,但在文本文件的开头又莫名其妙地多了一些字符。
发表于:2008-01-19 13:55:217楼 得分:0
lz,你的代码写文件后,就多出chr(10)及chr(13)
属于文件的换行符
发表于:2008-01-21 09:02:248楼 得分:0
如都用文本方式读写,连替换都不能进行。
------------------
什么意思?为什么不能替换?
vbscript code
private sub tihuan(tfilepath as string, oldstr as string, newstr as string) dim k as string, s as string open tfilepath for input as #1 do while not eof(1) ' 循环至文件尾。 s = input(1, #1) ' 读入一个字符。 k = k + s loop k = replace(k, oldstr, newstr) '无条件全部替换 close #1 open tfilepath for output as #2 print #2, k; close #2 end sub
发表于:2008-01-21 11:45:589楼 得分:0
print     改为   write   试下!

好象print写入   的   后面自动加上点什么..   而用另一个就不会有这样的情况   好象是   write吧     好象又是   put..   不太记得了!
发表于:2008-01-21 12:04:2910楼 得分:0
9楼,print改为write,会在整个文件前后加上一对""   。
发表于:2008-01-21 12:25:0911楼 得分:0
print的改成二进制打开使用put
发表于:2008-01-21 13:19:3112楼 得分:0
试试:

open   tfilepath   for   output   as   #2  
put   #2,   k  
close   #2  



open   tfilepath   for   output   as   #2  
print   #2,   k;  
close   #2
发表于:2008-01-21 13:21:1313楼 得分:0
更正:
open   tfilepath   for   output   as   #2      
put   #2,   0,   k      
close   #2
发表于:2008-01-21 13:35:2714楼 得分:0
那么说应该是   put   了!!

我只记得以前看过的一篇文章   说解决这个问题的   不能用   print   ..要哪个记不太清楚..
发表于:2008-01-21 13:55:3715楼 得分:0
。。。。。。。。。。。。
发表于:2008-01-21 14:01:3416楼 得分:0
我试了下8楼代码可用。
发表于:2008-01-21 14:41:5517楼 得分:0
你这个情况估计文本本身尾部有个回车符,所以替换文件后会不断增加,如下试试:

vbscript code
private sub tihuan(tfilepath as string,oldstr as string, newstr as string) dim k() as byte open tfilepath for binary as #1 redim k(filelen(tfilepath)-1) get #1, , k close #1 k=replacemid(strconv(k,vbunicode),1,len(strconv(k,vbunicode))-1) _ ,oldstr,newstr) '无条件全部替换 open tfilepath for output as #2 print #2, k close #2 end sub '看你的代码完全是字符处理,所以可以这样: private sub tihuan(tfilepath as string,oldstr as string, newstr as string) dim s as string open tfilepath for binary as #1 s=space(lof(#1)) get #1, , s close #1 s=replacemid(s,1,len(s)-1),oldstr,newstr) '无条件全部替换    open tfilepath for output as #2 print #2, s close #2 end sub
发表于:2008-01-22 21:49:0718楼 得分:0
楼上(17楼)的分析应该是对的,我试了下能行。
发表于:2008-01-22 21:53:2219楼 得分:0
楼上(17楼)的分析应该是对的,我试了下能行。
发表于:2008-01-22 21:55:5020楼 得分:0
但17楼的这句:
s=space(lof(#1))
应该为下面一句吧?
s   =   space(lof(1))


快速检索

最新资讯
热门点击