๔/๑๙/๒๕๕๐

Subject: Re: Visual Basic Binary file access
Answered By: theta-ga on 02 Nov 2002 09:14 PST
Rated:4 out of 5 stars
Reading binary data in VB is really quite easy. I'm am providing some
example code here, with pointers to articles you might refer to in
case you want more examples or explainations.
Here, I will try to read a record ExampleData from a binary file.
Since you say that you already know the binary file record structure,
you can substitute my ExampleData structure with your own record
structure.
I have included code for both VB 6 & VB 7.NET
Explaination of code snippets : I try to read/write a fixed length
record from/to a binary file.The record contains a fixed length string
of 10 chars and an integer value. I first write one record of this
type to the file and then read one record from the file in binary
mode.I also show you how to get the length of an open file in VB.

CODE SNIPPETS FOR VB5/VB6
--------------------------
'Define ExampleData structure
Type Exampledata
Name as String * 10 'This is a fixed length string of 10
characters
Age as Integer
End Type
Dim myData As ExampleData ' Declare variable of type ExampleData

...

'Open Binary File
'FOr Binary Access, The Open function has the following format :
'Open filename For Binary As filenumber
Open "c:\BinaryFile.bin" For Binary As #1

'When writing to a binary file, we use the Put statement.
'Put #FileNumber, ByteNumber, VariableName
'ByteNumber is the file pos from which we start writing the data
Put #1,,myData ' Writes mydata in file at the current position

'Close the file
Close #1

....

'Read data from the file
Open "c:\BinaryFile.bin" For Binary As #1
'To read data, use the Get function
'Get #FileNumber, ByteNumber, VariableName
'Data is read from the binary file position specified by ByteNumber
Get #1,,myData 'Read in data from the current file pos
'Print Data
Debug.Print myData.Name
Debug.Print myData.Age
'To get the length of file, use the LOF(filenumber) function
Debug.Print "Length of the Binary File = " & LOF(1)
'Close File
Close #1

RELATED LINKS
-------------
- Binary Files - File Access by James Crowley
A very good tutorial On using Binary Files under VB. Highly
recommended!
( http://www.developerfusion.com/show/85/1/ )
- MSDN Help topic: "Using Binary File Access"
The standard MS Help Article on Binary files in VB 6
( http://msdn.microsoft.com/library/devprods/vs6/vbasic/vbcon98/vbconusingbinaryfileaccess.htm
)
- MS KnowledgeBase Q257794 - HOWTO: Use Binary File Access with
Visual Basic
Adds to the above MSDN Article. Provides example code for
variable length records access in a binary file using VB.
( http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q257794&
)
- MS Support : File Access with Visual Basic® for Applications
Although for VBA, the same code works for VB6. Provides a list of
all the functions that can be used with the Binary file Access mode in
VB.Also provides example code.
( http://support.microsoft.com/default.aspx?scid=/support/excel/content/fileio/fileio.asp
)
======================

CODE SNIPPETS FOR VB7.NET
-------------------------
'Define ExampleData structure
Structure Exampledata
Name as String 'Fixed length string of 10
characters
Age as Integer
End Type
Public myData As ExampleData ' Declare variable of type
ExampleData

...

'Open Binary File
'For Binary Access, use the FileOpen function which has the form :
'FileOpen(FileNumber, FileName, OpenMode.Binary)
FileOpen (1,"c:\BinaryFile.bin", OpenMode.Binary

'When writing to a binary file, we use the FilePut() function.
'FilePut( FileNumber,VariableName)
FilePut(1,myData) ' Writes mydata in file at the current position

'Close the file
FileClose(1)

....

'Read data from the file
FileOpen (1,"c:\BinaryFile.bin", OpenMode.Binary
'To read data, use the FileGet function
'FileGet (FileNumber, VariableName)
FileGet(1,myData) 'Read in data from the current file pos
'Display length of open file using the func. LOF(filenumber)
Debug.WriteLine(LOF(1))
'Close File
FileClose(1)

RELATED LINKS
-------------
- MSDN VB.NEt File Functions Reference
- FileOpen()
( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmopen.asp
)
- FilePut()
( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vastmput.asp
)
- FileGet()
( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctget.asp)
- LOF()
( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/vafctlof.asp
)

MSDN Article : Binary File Access
( http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconopeningfileforbinaryaccess.asp
)

===========================
I hope the above answer will satisfy your needs.
If you need any clarifications, just ask.
Till the, Happy Coding !
:)