㈠ 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()函數生成要讀取的長度的串,或者使用定長串。