binary file

Posted on 2000-04-07
Last Modified: 2010-05-02
I have 8 variables that I want to load into an array from a binary file when the program is run.  Also, I am looking for the routine to read the 8 variable values in the array back into the binary file from a command click.
Question by:Daron1
  • 5
  • 3
LVL 14

Accepted Solution

wsh2 earned 10 total points
Comment Utility
You have a couple of choices..

Open strFilePath For Binary Access Read As #1

1. Read the fields individually

  Get #1, , variable1
  Get #1, , variable2
  Get #1, , variable3

2. Read the Fields as an Array (Data Type casting must be as it was written to Disk:

  Dim MyInput As Integer
  Redim MyInput(8)
  Get #1, , MyInput()

3. Set up a User Data Type and read into it.

  ' In Declarations section
  Private Type MyRecord
    variable1 as Integer
    variable2 as String * 30
    variable3 as Long
  End Type

  Dim MyInput as MyRecord
  Get #1, , MyInput
  Msgbox (MyInput.variable1)


LVL 14

Expert Comment

Comment Utility
To write to disk.. change the Open Access Clause to Read Write or Write.. and change the Get(s) to Put(s). If you use Read Write Access.. do not forget to use Seek(s) to position yourself properly in the file before writing.

Author Comment

Comment Utility
does the "get #1, var1" get the first line in the file followed by "get #1, var2" which gets the second line in the file and so on?  I think this is the idea but I'm not sure.
LVL 14

Expert Comment

Comment Utility
In example 1.. you are doing 1 read and 1 write for each field.

In Examples 2 and 3.. you are doing all 8 fields in one read / one write.
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.


Author Comment

Comment Utility
suppose in the first example I wanted to only get variable2 from the file, and the only write the variable2 variable back to the file.  I believe it is some sort of random access, but I'm not sure.
LVL 14

Expert Comment

Comment Utility
Unless your files are VERY large, you are better off, reading and writing all the values in one shot.. <smile>. ie..

At the beginning of the program:
Open File Access Read, input the whole File, and then close the file.

While your program is running:
Update the variables as necessary.

At the end of your program:
Open File Access Write, output all the variables back out.

To make variable access easy throughout your program declare them as module level variables.. and then you will be able to access them from anywhere in that form.

With very large files, yes, you would  want to consider implementing Random Access processing. The rule for random access processing though is that all records MUST be equal length.. and in that case you would do a Seek command to position yourself correctly before reading or writing. If all your variables are the same length, then you could open your file Random Read Write and individually address each stored variable. Although, since you have so few Variables to store (I assume), I personnally would NOT use this method.. but rather read / write the files all in one shot. Nevertheless, the syntax for positioning anywhere in a open file is..

From MSDN:
Seek Statement

Sets the position for the next read/write operation within a file opened using the Open statement.


Seek [#]filenumber, position

The Seek statement syntax has these parts:

filenumber: Required. Any valid file number.

position: Required. Number in the range 1 – 2,147,483,647, inclusive, that indicates where the next read/write operation should occur.


Record numbers specified in Get and Put statements override file positioning performed by Seek.

Seek Statement Example:

This example uses the Seek statement to set the position for the next read or write within a file. This example assumes TESTFILE is a file containing records of the user-defined type Record.

Type Record   ' Define user-defined type.
   ID As Integer
   Name As String * 20
End Type

For files opened in Random mode, Seek sets the next record.

Dim MyRecord As Record, MaxSize, RecordNumber   ' Declare variables.

' Open file in random-file mode.
Open "TESTFILE" For Random As #1 Len = Len(MyRecord)
MaxSize = LOF(1) \ Len(MyRecord)   ' Get number of records in file.

' The loop reads all records starting from the last.
For RecordNumber = MaxSize To 1 Step - 1
   Seek #1, RecordNumber   ' Set position.
   Get #1, , MyRecord   ' Read record.
Next RecordNumber

Close #1   ' Close file.

For files opened in modes other than Random mode, Seek sets the byte position at which the next operation takes place. Assume TESTFILE is a file containing a few lines of text.

Dim MaxSize, NextChar, MyChar
Open "TESTFILE" For Input As #1   ' Open file for input.
MaxSize = LOF(1)   ' Get size of file in bytes.

' The loop reads all characters starting from the last.
For NextChar = MaxSize To 1 Step -1  
   Seek #1, NextChar   ' Set position.
   MyChar = Input(1, #1)   ' Read character.
Next NextChar

Close #1   ' Close file.

Performing a file-write operation after a Seek operation beyond the end of a file extends the file. If you attempt a Seek operation to a negative or zero position, an error occurs


Author Comment

Comment Utility
I really appreciate the time and effort in helping me with this.  You've cleared up just what I was unsure of.  Many thanks...Daron

LVL 14

Expert Comment

Comment Utility
Go get em Daron!!!... <smile>

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now