Visual Basic Programming Code Examples
Visual Basic > Files Directories Drives Code Examples
Reading and writing binary data from files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
Reading and writing binary data from files
The following routines demonstrate how to write/read data to/from a file. The data can be an array or any simple data type.
Option Explicit
'Purpose : Saves/writes a block of data to a file
'Inputs : vData The data to store in the file. Can be an
' array or any simple data type.
' sFileName The path and file name where the data is to be stored
' [bAppendToFile] If True will append the data to the existing file
'Outputs : Returns True if succeeded in saving data
'Notes : Saves data type (text and binary).
Function FileWriteBinary(vData As Variant, sFileName As String, Optional bAppendToFile As Boolean = True) As Boolean
Dim iFileNum As Integer, lWritePos As Long
On Error GoTo ErrFailed
If bAppendToFile = False Then
If Len(Dir$(sFileName)) > 0 And Len(sFileName) > 0 Then
'Delete the existing file
VBA.Kill sFileName
End If
End If
iFileNum = FreeFile
Open sFileName For Binary Access Write As #iFileNum
If bAppendToFile = False Then
'Write to first byte
lWritePos = 1
Else
'Write to last byte + 1
lWritePos = LOF(iFileNum) + 1
End If
Put #iFileNum, lWritePos, vData
Close iFileNum
FileWriteBinary = True
Exit Function
ErrFailed:
FileWriteBinary = False
Close iFileNum
Debug.Print Err.Description
End Function
'Purpose : Reads the contents of a binary file
'Inputs : sFileName The path and file name where the data is stored
'Outputs : Returns an array containing all the data stored in the file.
' e.g. ArrayResults(1 to lNumDataBlocks)
' Where lNumDataBlocks is the number of data blocks stored in file.
' If the file was created using FileWriteBinary, this will be the number
' of times data was appended to the file.
Function FileReadBinary(sFileName As String) As Variant
Dim iFileNum As Integer, lFileLen As Long
Dim vThisBlock As Variant, lThisBlock As Long, vFileData As Variant
On Error GoTo ErrFailed
If Len(Dir$(sFileName)) > 0 And Len(sFileName) > 0 Then
iFileNum = FreeFile
Open sFileName For Binary Access Read As #iFileNum
lFileLen = LOF(iFileNum)
Do
lThisBlock = lThisBlock + 1
Get #iFileNum, , vThisBlock
If IsEmpty(vThisBlock) = False Then
If lThisBlock = 1 Then
ReDim vFileData(1 To 1)
Else
ReDim Preserve vFileData(1 To lThisBlock)
End If
vFileData(lThisBlock) = vThisBlock
End If
Loop While EOF(iFileNum) = False
Close iFileNum
FileReadBinary = vFileData
End If
Exit Function
ErrFailed:
Close iFileNum
Debug.Print Err.Description
End Function
'Demonstration routine
Sub Test()
Dim avValues() As Long, lThisRow As Long
Dim avFileData As Variant, vThisBlock As Variant, vThisSubBlock As Variant
Dim lThisBlock As Long
'Create an array of numbers
ReDim avValues(1 To 10)
For lThisRow = 1 To 10
avValues(lThisRow) = lThisRow
Next
'Write the array to a file
If FileWriteBinary(avValues, "C:\Test.dat") Then
'Read the data back from the file
avFileData = FileReadBinary("C:\Test.dat")
If IsArray(avFileData) Then
'Print data
Debug.Print "Values returned:"
For Each vThisBlock In avFileData
lThisBlock = lThisBlock + 1
Debug.Print "Data Set:" & CStr(lThisBlock)
For Each vThisSubBlock In vThisBlock
Debug.Print vThisSubBlock
Next
Next
'Completed
MsgBox "The array has been successfully retrieved!", vbInformation
End If
End If
End Sub