Private Sub Command1_Click()
Dim FF As Integer
Dim lLenB As Long
Dim bHdr(15) As Byte
Dim bDat(32) As Byte
Dim i As Long
FF = FreeFile
Open App.Path & "\020710.txt" For Binary Access Read As FF
lLenB = LOF(FF) - 16
Debug.Print "lLenB\33", lLenB \ 33
Get FF, , bHdr
Debug.Print "bHdr", StrConv(bHdr, vbUnicode)
Debug.Print "-----"
For i = 0 To lLenB \ 33
Get FF, , bDat
Debug.Print "bDat", StrConv(bDat, vbUnicode)
Next
Close FF
End Sub
Option Explicit
Private Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Private Sub Command1_Click()
Dim FF As Integer
Dim lLenB As Long
Dim bHdr(15) As Byte
Dim bDat(32) As Byte
Dim strDat As String
Dim dblDat As String
Dim i As Long
FF = FreeFile
Open App.Path & "\020710.txt" For Binary Access Read As FF
'Get length of file in bytes less header length.
lLenB = LOF(FF) - 16
'Calculate how many 33 bytes records are in file
Debug.Print "lLenB\33", lLenB \ 33
Get FF, , bHdr
Debug.Print "bHdr", StrConv(bHdr, vbUnicode)
Debug.Print "-----"
'Step through records
For i = 0 To lLenB \ 33
Get FF, , bDat 'Read 33 byte record
strDat = StrConv(bDat, vbUnicode) 'Convert to string.
'First 4 characters is field 1.
'Characters 5 to 16 is field 2
'Field 3 is double (8 bytes) extracted starting at byte 17.
'Field 4 is last character which is &H0F. Adjusting for base 0 it is 16.
Debug.Print "bDat", Left$(strDat, 4), Mid$(strDat, 5, 12), ByteArrayToDouble(bDat), Asc(Right$(strDat, 1)) + 1
Next
Close FF
End Sub
Function ByteArrayToDouble(ByRef aByte() As Byte) As Double
'Each record is 33 bytes. Floating point Double starts at byte 17 and i 8 bytes inlength.
'Adjusting for base 0 we copy bytes to a double variable.
CopyMemory ByteArrayToDouble, aByte(16), 8
End Function
Dim FF As Integer
'to retrieve file
Dim lLenB As Long
'byte array to retrieve first record
Dim bHdr(15) As Byte
'byte array to retrieve body records
Dim bDat(32) As Byte
'to save the body records in unicode
Dim strDat As String
'no function
'Dim dblDat As String
Dim i As Long
FF = FreeFile
Open FileName For Binary Access Read As FF
'LOF= length of file in bytes minus the header (which is 16 bytes)
lLenB = LOF(FF) - 16
'recordlength is LOF divided by 33 (2*16+1) by use of the integer division operator
'Integer division is carried out using the \ Operator. Integer division
'returns the quotient, that is, the integer that represents the number of
'times the divisor can divide into the dividend without consideration of any remainder.
Debug.Print "lLenB\33", lLenB \ 33
Get FF, , bHdr
'converting a byte array to unicode (readable English)
Debug.Print "bHdr", StrConv(bHdr, vbUnicode)
Debug.Print "-----"
'Stepping through the file in blocks of 33 bytes
For i = 0 To lLenB \ 33
Get FF, , bDat
'building data record in readable English
strDat = StrConv(bDat, vbUnicode)
'printing the bytes using $-functions ($= to handle bytes instead of characters?)
'but strDat is already converted to string. Is there another reason to use the
'$-function.
Debug.Print "bDat", Left$(strDat, 4), Mid$(strDat, 5, 12), ByteArrayToDouble(bDat), Asc(right$(strDat, 1)) + 1
Next
Close FF
End Sub
Function ByteArrayToDouble(ByRef aByte() As Byte) As Double
'convert starting byte 16 with a length of 8 bytes.
CopyMemory ByteArrayToDouble, aByte(16), 8
End Function
Option Explicit
Private Type udtRecord
sField1 As String * 4
sField2 As String * 12
dField3 As Double '8 bytes
dField4 As Double '8 bytes
sField5 As Byte
End Type
Private Sub Command2_Click()
Dim FF As Integer
Dim lRows As Long
Dim bHdr(15) As Byte
Dim bDat As udtRecord
Dim i As Long
FF = FreeFile
Open App.Path & "\020710.txt" For Binary Access Read As FF
lRows = (LOF(FF) - 16) \ Len(bDat) 'Calculate how many 33 bytes records are in file
Debug.Print "lRows", lRows
Get FF, , bHdr
Debug.Print "bHdr", StrConv(bHdr, vbUnicode)
Debug.Print "-----"
For i = 0 To lRows 'Step through records
Get FF, , bDat 'Read record
Debug.Print bDat.sField1, bDat.sField2, bDat.dField3, bDat.sField5 + 1
Next
Close FF
End Sub
aaau,646945,6.7700000,,2
aaau,85830,0.0250000,,2
aaau,922974,2.4000000,,2
aaau,945925,0.7800000,,2
aaau,GRR8,2.2200000,,2
aaau,TZ,2.7500000,,2
aaau,TZL,2.1000000,,2
aaau,alk,0.2800000,,2
aaau,bsl,6.9800000,,2
aaau,ced,0.1500000,,2
aaau,chb,42.5300000,,2
aaau,ghg,1.2250000,,2
aaau,ral,0.2700000,,2
aaau,sbd,0.0900000,,2
aaau,ses,0.0800000,,2
aaau,swt,0.2100000,,2
aaau,tls,3.6700000,,2
aaau,wmc,9.4600000,,2
Fields are:
text, text or LONG number, FLOAT number, null, and the number 2.
Don't know what it's used for...