binary file

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.
Who is Participating?
wsh2Connect With a Mentor Commented:
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)


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.
Daron1Author Commented:
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.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.
Daron1Author Commented:
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.
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

Daron1Author Commented:
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

Go get em Daron!!!... <smile>
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.