VBA-二进制文件的读取(仅限小文件)

Sub 二进制读取()
    Dim filename As String
     filename = ThisWorkbook.path & "\text.txt"
     Open filename For Binary As #1 '打开文件,可以对文件进行读写操作
     Dim chs() As Byte, i As Integer, k As Integer
     For i = 0 To LOF(1) - 1 '循环至文件末端
     k = k + 1
     ReDim chs(1 To k) As Byte '将文件内容存入字节数组
     Get #1, , chs(k) '获取文本内容
     Next i
     Close #1
     MsgBox chs
    
 End Sub1.将文件的数据读取出来,其代码和解释如下

2.二进制文件的写入

Sub 写入二进制文件()
     Dim filename As String
     filename = ThisWorkbook.path & "\text.txt"
     Open filename For Binary As #1
   
    Dim s As String
    s = "今天天气真好"
    Put #1, , s '将内容写入文件
    Close #1
   
 
 End Sub

3.二进制文件的复制

Sub 复制文件() '将#1的文件内容复制到#2
    Dim filename As String, filetarget As String
    filename = ThisWorkbook.path & "\text.txt"
    filetarget = ThisWorkbook.path & "\sdd.txt"
    Open filename For Binary As #1
    Open filetarget For Binary As #2
    Dim i As Integer, ch As Byte
    For i = 0 To LOF(1) - 1
        Get #1, , ch
        Put #2, , ch
    Next i
    Close #1, #2
 End Sub

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值