Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


binary file

Posted on 2000-04-07
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 14

Accepted Solution

wsh2 earned 40 total points
ID: 2693446
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

ID: 2693458
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

ID: 2696193
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.
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

LVL 14

Expert Comment

ID: 2696293
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.

Author Comment

ID: 2696342
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

ID: 2696560
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

ID: 2696664
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

ID: 2696713
Go get em Daron!!!... <smile>

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

618 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