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



关于vb二进文件读取的问题


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


关于vb二进文件读取的问题
发表于:2007-07-04 16:04:41 楼主
有一个程序,想改掉内部的一点东西,重新生成一个新的程序

先要读取出资源文件1的全部数据,然后查找出资源文件1特征a.a.a.a并替换成b.b.b.b然后重新生成一个新文件,a.a.a.a的位置不定,但一定有这个特征!求各位帮帮忙,想了一个下午!
发表于:2007-07-04 16:11:571楼 得分:0
读到内存byte数组中,然后遍历这个数组,符合条件后替换掉内容

完成后将byte数组重新写回文件即可。

因是二进制文件,读到   byte数组中后应该使用字节比较,不要使用字符串比较。
发表于:2007-07-04 16:12:492楼 得分:0
能否写个代码给我看看
发表于:2007-07-04 16:23:073楼 得分:0
str1=text1.text

cdlocation.filter   =   "可执行文件(*.exe) ¦*.exe "
        cdlocation.showsave
        if   cdlocation.filename   =   " "   then   exit   sub
       
    on   error   goto   myerr:
   
        bfile   =   loadresdata(101,   "custom ")                     '读出101号资源
   
       
        open   cdlocation.filename   for   binary   access   write   as   #1
x
x
x
x
x
        close   #1
exit   sub
myerr:
        reset       '如果有文件未关闭,则关闭之
        msgbox   err.description   &   "   出错. "
end   sub


忘大侠帮帮完善
发表于:2007-07-04 16:31:254楼 得分:0
先搞清楚你要用一个程序读取另一个单独的文件,还是要用程序读取自身的资源?两码事啊!

你用   loadresdata   只能载入当前   exe   的自身资源文件,这玩意改了不经过编译怎么可能直接写回exe中去呢?
发表于:2007-07-04 16:43:305楼 得分:0
'引入文件对话框部件,并命名为cdlocation
        cdlocation.filter   =   "可执行文件(*.exe) ¦*.exe "
        cdlocation.showsave
        if   cdlocation.filename   =   " "   then   exit   sub
        on   error   goto   myerr:
        bfile   =   loadresdata(101,   "custom ")                     '读出101号资源
        open   cdlocation.filename   for   binary   access   write   as   #1
                for   lfile   =   0   to   file_size   -   165
                        put   #1,   ,   bfile(lfile)
                next   lfile
                for   i   =   1   to   iinfolen           '读出配置信息并追加
                        str1   =   mid(txtinfo.text,   i,   1)
                        if   asc(str1)   > =   0   then           '非中文
                              binfo   =   asc(str1)
                              put   #1,   ,   binfo
                        else                                               '中文
                              binfo   =   hibyte(asc(str1))             '先高位
                              put   #1,   ,   binfo
                              binfo   =   lowbyte(asc(str1))           '再低位
                              put   #1,   ,   binfo
                        end   if
                next   i
        close   #1
        exit   sub
myerr:
        reset       '如果有文件未关闭,则关闭之
        msgbox   err.description   &   "   配置信息请用半角字符. "
end   sub
这是别人的,不过是追加到尾部的,我的中间改一些字符,应该可以改的吧?
发表于:2007-07-04 16:53:436楼 得分:0
汗一个,楼主贴的两段代码理解起来完全不一样。

后面的代码是将当前exe文件中的101号资源写到一个目标文件中去,前面的代码不知所以然。然后前贴又提到资源文件什么的在里面掺和,让人不明白。


其实问题是不是可以简单的描述为:
有个二进制文件a,要将里面所有的bb内容替换成cc,然后生成d文件?
发表于:2007-07-04 17:52:217楼 得分:0
恩,是的。
发表于:2007-07-04 19:15:468楼 得分:0
option   explicit

'功能:以字节方式替换文件内容后输出
'参数:源文件名,输出文件名,查找内容,替换内容
'注意:替换内容是区分大小写的
'               可以处理任何类型文件,但如果处理的是可执行类二进制文件
'               有可能造成破坏而不能正常运行
'
'调用示例:
'               将文件   abc.txt   中的   "ab "   字符全部替换为   "12 "
'               bytereplace   "c:\abc.txt ", "c:\abc2.txt ", "ab ", "12 "
'
'代码编写:阿勇   fxy_2002@163.com
'日期:2007/07/04
'
sub   bytereplace(byval   sfile   as   string,   byval   ofile   as   string,   fstr   as   string,   rstr   as   string)
       
        dim   fbyte()   as   byte
        dim   fstrbyte()   as   byte,   rstrbyte()   as   byte
       
        fstrbyte   =   strconv(fstr,   vbfromunicode)
        rstrbyte   =   strconv(rstr,   vbfromunicode)
       
        dim   l   as   long
        l   =   ubound(fstrbyte)   +   1
       
        if   l   <>   ubound(rstrbyte)   +   1   then   exit   sub   '替换内容与被替换内容不等长,退出
       
        dim   f   as   integer
        dim   fl   as   long
       
        f   =   freefile()
       
        open   sfile   for   binary   as   #f
       
        fl   =   lof(f)
        redim   fbyte(fl   -   1)
       
        get   #f,   ,   fbyte
        close   #f
       
        dim   i   as   long,   j   as   long
        dim   b   as   boolean
       
        for   i   =   0   to   (fl   -   1)   -   (l   -   1)           '遍历整个文件字节数组
                if   fbyte(i)   =   fstrbyte(0)   then     '如果与第一个字节匹配,进入循环比较后续字节。
                                                                                '否则,从下一个字节开始重新匹配
                        b   =   true
                        for   j   =   1   to   (l   -   1)
                                if   fbyte(i   +   j)   <>   fstrbyte(j)   then
                                        b   =   false
                                        exit   for
                                end   if
                        next
                        if   b   then                                       '全部字节匹配,替换内容
                                for   j   =   i   to   i   +   (l   -   1)
                                        fbyte(j)   =   rstrbyte(j   -   i)
                                next
                                i   =   i   +   (l   -   1)                   '替换内容后,当前指针跳过已替换之内容,处理下一个匹配项
                        end   if
                end   if
        next
       
        f   =   freefile()
        open   ofile   for   binary   as   #f
        put   #f,   ,   fbyte
        close   #f
       
        erase   fbyte
        erase   fstrbyte
        erase   rstrbyte
             
end   sub


发表于:2007-07-04 19:18:119楼 得分:0
以上代码没经过严格的调试,拿了一个文本文件和一个rar文件测试,正确的处理了。
rar文件未损坏,可以正常解压;文本文件可正常读取

处理一个exe文件,可以处理但处理后不能运行。
发表于:2007-07-05 08:46:2510楼 得分:0
ok,已解决
发表于:2007-07-06 14:08:1311楼 得分:0
dim   l   as   long
l   =   ubound(fstrbyte)   +   1
有个疑问   如果文件很大   long不够用怎么办
发表于:2007-07-06 15:16:0112楼 得分:0
vb处理不了大于2g的文件。


快速检索

最新资讯
热门点击