导航:首页 > 文件教程 > vb二进制方式打开文本文件

vb二进制方式打开文本文件

发布时间:2022-12-30 07:40:46

㈠ vb.net 二进制读取文件

VB.NET打开二进制文件用fileopen完成,打开二进制文件的形式为:openmode.binary
读取二进制文件用的是fileget方法,写入二进制文件用的是fileput方法。
应用示例:将一批随机数保存在一个dat文件中,然后再将其提取到文本框中。
二进制文件的读写一批随机数的存取,程序为:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x, i, fn As Integer
Dim s As String = ""
fn = FreeFile()
FileOpen(fn, "d:\data.dat", OpenMode.Binary)
For i = 1 To 8
x = Int(Rnd() * 100)
s = s + Str(x)
FilePut(fn, x)
Next
FileClose(fn)
TextBox1.Text = s
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim x, fn As Integer
Dim s As String = ""
fn = FreeFile()
FileOpen(fn, "d:\data.dat", OpenMode.Binary)
Do While Not EOF(fn)
FileGet(fn, x)
s = s + Str(x) + " "
Loop
FileClose(fn)
TextBox1.Text = s
End Sub

㈡ vb 二进制打开文件

Dimfile()AsByte,MyFilesAsString
MyFiles="G:学习资料第六学期123.JPG"

OpenMyFilesForBinaryAs#1'使用二进制方法打开文件
ReDimfile(LOF(1)-1)AsByte'因为数组从0开始,所以这里数组上标要减去1
Get#1,,file'把文件内容存入数组
Close
Dima(1,1)
DimiAsInteger,jAsInteger
Fori=0To1
Forj=0To1
a(i,j)=file(179+i*(2-1)+j)
Next
Next

㈢ vb 二进制文件读写

Dim bytes() As Byte
dim lLength as Long
Open "c:\main.exe" For Binary As #1 '读取字节
lLength=LOF(1)
ReDim bytes(1 To lLength) As Byte
Get 1, , bytes
Close #1

'将文件以十六进制方式输出到Text1中,这个过程很耗时,130K的文件大概需要2分钟,而且越到后面速度越慢
Dim strTmp As String
For i = 1 To lLength
strTmp = strTmp & " " & Hex(bytes(i))
DoEvents
Next
Text1.Text = strTmp

Open "d:\abcd.exe" For Binary As #1 '将字节转存
Put 1, , bytes
Close #1

End Sub

㈣ 怎样用VB打开二进制文件,并用文本框显示出来

Private Sub Form_Load()
Dim Temp() As Byte
Dim Templen As Long
Templen = FileLen("c:\1.txt")
ReDim Temp(1 To Templen) As Byte
Open "c:\1.txt" For Binary As #1
Get #1, , Temp
Close #NUM
'''''''放到text中,不支持汉字不知道怎么样可以支持汉字''''''''''
For i = 1 To Templen
Text1.Text = Text1.Text & Chr(Temp(i))
Next i
''''''输出到文件,这个支持汉字和所有字符'''''''''''''''''''''''''''''''''''''''
Open "c:\2.txt" For Binary As #2
Put #2, , Temp
Close #2

End Sub

㈤ 如何用vb读写以二进制形式读写文件

使用随即方式读写,数据类型可以这样(如果都是第一个和第六个为长整形):
type xxx
p1 as long
p2 as single
p3 as single
p4 as single
p5 as single
p6 as long
end type
dim vxxx as xxx
打开文件
open filepath for access as filenum len=lenb(vxxx)
读写:
使用put和get
open filename$ for binary #filenumber'以二进制方式建立或者打开文件,然后再用
put #filenumber,postion,inputdata'写入数据inputdata
get #filenumber,postion,outputvarible'读出数据到outputvarible
这是我对二进制文件写的一段源码,你自已分析吧

'***************************************************************************************
' 目的: 将文件保存到数据库
' 方法: DAO
' 输入:
'***************************************************************************************
Public Sub SaveDataToRs(ByVal sGtXs As String)
Dim i As Integer
Dim myRs As Recordset, mySql As String
Dim DataFile As Integer
Dim Fragment As Integer, Fl As Long, Chunks As Integer
Dim Chunk() As Byte
Const ChunkSize As Integer = 1024

mySql = "select 图片 from 杆塔型式表 where 杆塔型式 like '" & sGtXs & "'"
Set myRs = myDb.OpenRecordset(mySql, dbOpenDynaset)

DataFile = 1
Open sFileName For Binary Access Read As DataFile

Fl = LOF(DataFile)
If Fl = 0 Then
Close DataFile
Set myRs = Nothing
MsgBox "文件长度为零!保存数据没有成功"
Exit Sub
End If

myRs.Edit
Chunks = Fl ChunkSize
Fragment = Fl Mod ChunkSize
ReDim Chunk(Fragment)
Get DataFile, , Chunk()
myRs!图片.AppendChunk Chunk()
ReDim Chunk(ChunkSize)
For i = 1 To Chunks
Get DataFile, , Chunk()
myRs!图片.AppendChunk Chunk()
Next i
Close DataFile
myRs.Update

myRs.Close
Set myRs = Nothing
End Sub

'***************************************************************************************
' 目的: 从数据库中读取数据(二进制)写入文件
' 方法: ADO
' 输入:
'***************************************************************************************
Public Sub ReadDataFromRs(ByVal sGtXs As String) '提取到文件
Dim cnn1 As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim strCnn As String
Dim DataFile As Integer
Dim Fragment As Integer, Fl As Long, Chunks As Integer
Dim Chunk() As Byte
Dim lngOffset As Long, lngTotalSize As Long
Dim strChunk As String
Const ChunkSize As Integer = 1024
Const rsField As String = "图片"

' 打开连接。
strCnn = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data source=" & App.Path & "pwmis.mdb;" 'SQL 连接为: strCnn = "Provider=sqloledb;" & "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
cnn1.Open strCnn
rs.Open "select 图片 from 杆塔型式表 where 杆塔型式 like '" & sGtXs & "'", cnn1

If Not rs.EOF Then
DataFile = 1
Open App.Path & "temp.tmp" For Binary Access Write As DataFile
lngTotalSize = rs.Fields(rsField).ActualSize
Chunks = lngTotalSize ChunkSize
Fragment = lngTotalSize Mod ChunkSize
ReDim Chunk(ChunkSize)
Chunk() = rs.Fields(rsField).GetChunk(ChunkSize)
Put DataFile, , Chunk()
lngOffset = lngOffset + ChunkSize
Do While lngOffset < lngTotalSize
Chunk() = rs.Fields(rsField).GetChunk(ChunkSize)
Put DataFile, , Chunk()
lngOffset = lngOffset + ChunkSize
Loop
Close DataFile
End If
rs.Close
Set rs = Nothing
End Sub

㈥ vb问题:请问怎么用二进制方式(binary)打开文本文件(txt)并读取里面的文本到文本框text1中

1、在工程菜单中 添加部件 找到 Microsoft common dialog control 选上添加。

2、在 form1 中 拖放 text、Command和commondialog 控件,把commondialog的name 属性改为 Cdg1。

3、在command1_click 事件中输入代码

Private Sub Command1_Click()

Dim FileNam As String

Dim tStr() As Byte

Dim txtStr As String

CDg1.Filter = "*.txt" ' 文件对话框的默认打开文件类型

CDg1.FileName = "*.txt"

CDg1.ShowOpen

FileNam = CDg1.FileName

Open FileNam For Binary As #1 '以二进制文件方式打开文件


ReDim tStr(n) As Byte

Get #1, , tStr '读取文本

Close (1)

Text1.Text = StrConv((tStr()), vbUnicode) ' 写入text1

End Sub

㈦ Vb问题,怎样用二进制打开文件

'一下内容全部复制到记事本中,保存为Form1.frm
新建工程 添加窗体 添加已存窗体 把这个文件添加进去

Option Explicit

#Const DebugMode = 1
Const csBYTELEN As Byte = 8

Public Function DbDisplayError(strM As String) As Boolean
Dim strT As String

strT = strM & vbCrLf & Err.Number & vbCrLf & Err.Description
#If DebugMode Then
MsgBox strT
#End If

SetLastError strT
End Function

Private Function ctrByte2String(ByVal bytSrc As Byte, ByRef strDest As String) As Boolean
On Error GoTo ErrHandler
Dim blnR As Boolean
Dim bytT As Byte, lngI As Long, lngT As Long
Dim strT As String

strDest = " "
For lngI = 7 To 0 Step -1
bytT = Fix(bytSrc / (2 ^ lngI))
bytSrc = bytSrc Mod (2 ^ lngI)
strDest = strDest & CStr(bytT)
Next
blnR = True
ErrHandler:
If Err Then
DbDisplayError "ctrByte2String "
blnR = False
End If
ctrByte2String = blnR
End Function
'字符串必须为csBYTELEN个长度,并且必须为0和1
Private Function ctrString2Byte(ByVal strSrc As String, ByRef bytDest As Byte) As Boolean
On Error GoTo ErrHandler
Dim blnR As Boolean
Dim lngI As Long, strT As String
Dim bytT As Byte

bytDest = 0
For lngI = 1 To csBYTELEN
bytT = 0
strT = Mid(strSrc, lngI, 1)
If strT = "1 " Then
bytT = 1
End If
bytDest = bytDest * 2 + bytT
Next
blnR = True
ErrHandler:
If Err Then
DbDisplayError "ctrString2Byte "
blnR = False
End If
ctrString2Byte = blnR
End Function

Private Sub cmdExit_Click()
Unload Me
End Sub

Private Sub cmdOpen_Click()
On Error GoTo ErrHandler
Dim blnR As Boolean
Dim intH As Integer
Dim lngI As Long, lngL As Long
Dim bytAry() As Byte, bytT As Byte
Dim strDest As String, strT As String
Dim strFn As String

'打开文件
strFn = "c:\ipconfig.txt "
intH = FreeFile()
If Dir(strFn) = " " Then GoTo ErrHandler

'读取文件
lngL = FileLen(strFn)
Debug.Assert lngL <> 0
ReDim bytAry(lngL - 1)
Open strFn For Binary As #intH
Get #intH, , bytAry()
Close #intH
intH = 0

'转换内容
For lngI = 0 To lngL - 1
strT = " "
bytT = bytAry(lngI)
blnR = ctrByte2String(bytT, strT)
strDest = strDest & strT
Next lngI

'显示内容
Me.txtContent = strDest
'

blnR = True
ErrHandler:
If Err Then
DbDisplayError "cmdOpen_Click "
blnR = False
End If
If intH <> 0 Then Close intH
End Sub

Private Sub cmdSave_Click()
On Error GoTo ErrHandler
Dim blnR As Boolean
Dim intH As Integer
Dim lngI As Long, lngL As Long
Dim bytAry() As Byte, bytT As Byte
Dim strDest As String, strT As String
Dim strFn As String

'显示内容
strDest = Trim(Me.txtContent)
'确保字符串长度为八的整数倍
lngL = Len(strDest) / csBYTELEN
strDest = Mid(strDest, 1, lngL * csBYTELEN)

'
Debug.Assert lngL <> 0
ReDim bytAry(lngL - 1)
'转换内容
For lngI = 0 To lngL - 1
strT = Mid(strDest, lngI * csBYTELEN + 1, csBYTELEN)
blnR = ctrString2Byte(strT, bytAry(lngI))
Debug.Assert blnR
Next lngI

'打开文件
strFn = App.Path & "\test.txt "
intH = FreeFile()
If Dir(strFn) = " " Then GoTo ErrHandler

'写入文件
Open strFn For Binary As #intH
Put #intH, , bytAry()
Close #intH
intH = 0

blnR = True
ErrHandler:
If Err Then
DbDisplayError "cmdSave_Click "
blnR = False
End If
If intH <> 0 Then Close intH
End Sub

Private Sub Form_Load()

End Sub

㈧ vb二进制打开文件,在第10个位置写入固定内容该怎么写

dim ins,temp(10000),i% ,j% '定义变量
ins="98 57 84 14 21 25 55 44 10 12" '赋值
open "1.exe" for binary as #1 '2进制方式打开文件
do until eof(1) '开始循环,循环到文件尾
i=i+1 '循环变量递增
input #1, ,temp(i) '读入文件内容
loop '继续循环
close #1 '关闭文件
open "1.exe" for binary as #2 '2进制打开文件,准备写入
for j=1 to i '循环
print #2,temp(j) '写入新数据
if j=10 then print #2,ins '当写到第10个时,写入新内容
next j '循环

close #2 '关闭文件

㈨ 以二进制的形式打开并读取文件(VB).

以下是示例,请参考

本示例示范 Open 语句的不同用法来做到文件的输出与输入。

下列代码以顺序输入模式打开 TESTFILE 文件。

Open "TESTFILE" For Input As #1
' 若要以其他方式打开文件,必需先关闭此文件。
Close #1

下列代码以只允许写操作的二进制方式打开文件。

Open "TESTFILE" For Binary Access Write As #1
' 若要以其他方式打开文件,必需先关闭此文件。
Close #1

下列代码以随机方式打开文件,文件中含有用户自定义数据类型 Record 的记录。

Type Record ' 定义用户自定义数据类型。
ID As Integer
Name As String * 20
End Type

Dim MyRecord As Record ' 声明变量。
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
' 若要以其他方式打开文件,必需先关闭此文件。
Close #1

下列代码以顺序输出方式打开文件;任何过程都可以读写该文件。

Open "TESTFILE" For Output Shared As #1
' 若要以其他方式打开文件,必需先关闭此文件。
Close #1

下列代码以只允许读的二进制方式打开文件;其他过程不可以读该文件。

Open "TESTFILE" For Binary Access Read Lock Read As #1

㈩ vb如何编程二进制文件

1、用open打开二进制文件,用put写文件,用get读文件。

2、所需工具:vb6

3、示例:(二进制文件的写出与读取)

PrivateSubCommand1_Click()
Open"c:111.txt"ForBinaryAccessWriteAs#1
DimsAsString
Dimn1AsInteger,n2AsLong
Dimf1AsSingle,f2AsDouble

s="12345"
n1=10
n2=50000
f1=0.1
f2=0.0000000001
Put#1,,s
Put#1,,n1
Put#1,,n2
Put#1,,f1
Put#1,,f2

Close#1
Prints,n1,n2,f1,f2
EndSub

PrivateSubCommand2_Click()
Open"c:111.txt"ForBinaryAccessReadAs#1
DimsAsString
Dimn1AsInteger,n2AsLong
Dimf1AsSingle,f2AsDouble

s=String(5,0)
Get#1,,s
Get#1,,n1
Get#1,,n2
Get#1,,f1
Get#1,,f2

Close#1
Prints,n1,n2,f1,f2
EndSub

4、注意:读取字符串时,提前用string()函数生成要读取的长度的串,或者使用定长串。

阅读全文

与vb二进制方式打开文本文件相关的资料

热点内容
经营如何让数据说话 浏览:258
如何在手机上升级opop 浏览:614
coreldrawx5免费视频教程 浏览:725
网站引导页面源码 浏览:234
个人简历范文word 浏览:220
uc下载的视频怎样提取到文件 浏览:499
英雄下载下载最新版本2015下载安装 浏览:433
NX深孔钻编程替换面如何操作 浏览:725
手机怎么删除pdf文件 浏览:256
苹果手机没有efs文件夹怎么办 浏览:723
metro软件在哪个文件夹 浏览:69
怎么用手机登录编程猫 浏览:400
文本md204显示器如何编程 浏览:705
如何将表中重复数据标记 浏览:859
中级数据库系统工程师应用技术考什么 浏览:404
博途编程如何设置停止键 浏览:409
python3删除文件内容 浏览:754
如何优化seo数据分析 浏览:132
64位win7下部分32位程序不能运行 浏览:206
dnf90版本剑魂钝器流 浏览:649

友情链接