请教高手,二进制文件的读取问题,附源码

bluearc888 2004-09-19 06:50:51
一个二进制文件,文件尾部的35(有时是38,有时是40,问题就在这里)字节处有诸如abcde的字符。我想写一个程序把它读取并显视出来。程序如下:
Private Sub cmdGet_Click()
Dim sBuf As String * 38 ' !!!请高手注意这里:当"abcde"等字符在38字节以后时就什么也不能显视了!在38字节以前则显视不全。
Dim iFile As Long ' 可用的文件号
Dim nFileLen As Long ' 文件长度
Dim strAppExe As String ' 可执行文件名
' 获取文件名称
strAppExe = App.Path
If Right(strAppExe, 1) <> "\" Then
strAppExe = strAppExe & "\"
End If
strAppExe = "D:\bake.bin"
' 获取可用的文件号
iFile = FreeFile()
' 打开文件
Open strAppExe For Binary As #iFile
Get #iFile, LOF(iFile) - Len(sBuf) - 1, sBuf
Close #iFile

Text1.Text = sBuf'经常不能显视或显视不全,如果这里用me.print sBuf来代替则能打印在窗体上。当然有一些字符也是怪怪的。
Print sBuf
End Sub
请教高手!如何准确地得到文件尾部的字符信息并显视!
...全文
217 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wumylove1234 2004-09-19
  • 打赏
  • 举报
回复
mark一下!
caojinrong 2004-09-19
  • 打赏
  • 举报
回复
走过,路过,错过,走了。
VirtualAlloc 2004-09-19
  • 打赏
  • 举报
回复
应该是:
dim bytBuf() as byte
dim sBuf as string

strAppExe = "D:\bake.bin"
iFile = FreeFile()
Open strAppExe For Binary As #iFile
redim bytBuf(LOF(iFile)-38)
Get #iFile, 38 , bytBuf
Close #iFile

sBuf=strconv(bytBuf,vbUnicode)
me.print sBuf
rainstormmaster 2004-09-19
  • 打赏
  • 举报
回复
大致就是这个意思

//一个二进制文件,文件尾部的35(有时是38,有时是40,问题就在这里)字节处有诸如abcde的字符

这个数据到底是什么
northwolves 2004-09-19
  • 打赏
  • 举报
回复
try:

Private Sub cmdGet_Click()
Dim sBuf() As Byte
Open "D:\bake.bin" For Binary As #1
ReDim sBuf(38)
Get #1, LOF(1) - 38, sBuf
Close #1
Text1.Text = StrConv(sBuf, vbUnicode)
Erase sBuf
End Sub
rainstormmaster 2004-09-19
  • 打赏
  • 举报
回复
Private Sub cmdGet_Click()
Dim sBuf() As byte' 定义为byte数组
redim sBuf(37)
Dim iFile As Long ' 可用的文件号
Dim nFileLen As Long ' 文件长度
Dim strAppExe As String ' 可执行文件名
' 获取文件名称
strAppExe = App.Path
If Right(strAppExe, 1) <> "\" Then
strAppExe = strAppExe & "\"
End If
strAppExe = "D:\bake.bin"
' 获取可用的文件号
iFile = FreeFile()
' 打开文件
Open strAppExe For Binary As #iFile
Get #iFile, LOF(iFile) - ubound(sBuf) , sBuf
Close #iFile
dim s as string
s=strconv(sbuf,vbunicode)
Text1.Text = s
Print s
End Sub

7,762

社区成员

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

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