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
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 10 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.
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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 utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

690 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