Solved

binary file

Posted on 2000-04-07
8
229 Views
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.
0
Comment
Question by:Daron1
[X]
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
8 Comments
 
LVL 14

Accepted Solution

by:
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)



 


0
 
LVL 14

Expert Comment

by:wsh2
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.
0
 

Author Comment

by:Daron1
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:wsh2
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.
0
 

Author Comment

by:Daron1
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.
0
 
LVL 14

Expert Comment

by:wsh2
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.

Syntax

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.

Remarks:

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


0
 

Author Comment

by:Daron1
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

0
 
LVL 14

Expert Comment

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

Featured Post

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.

Question has a verified solution.

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

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

739 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