Binary File Input/Output.

Posted on 1998-07-07
Medium Priority
Last Modified: 2010-05-03
Heres my problem:  
I have a routine that gives me variable length binary data. I wish to store that in a small file and
then read it back. I don't want to use Random file access because of the fixed length read/write;
The binary file must be smaller than that.    Here is what I tried (It doesn't work):

Open OutFile For Binary As #1        ' Open Output File
 Put #1, ,MyData1       ' Variable Length Binary Data
 Put #1, ,MyData2
 Put #1, ,MyData3
 Close #1    ' Close Input

Open InFile For Binary As #1
' What do I do here?
' if I use    Mystring = Input(1,#1)
' then I will only get one char or byte.
' I need to read in each string of binary data as it was written!
' You can't use record numbers in binary mode, just in Random (fixed length)mode.

Close #1
Question by:jgore
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

Expert Comment

ID: 1465166
Question is already been posted

Expert Comment

ID: 1465167
You can use this code to read the entire file and put it in a binary array variable:

    Dim FContents() as Byte
    'Read the file contents using binary input
    Open FileName For Binary Access Read As #1
    FContents = InputB(LOF(1), 1)
    Close 1
As far as I know, you can not determine how binary data was written to the file, I mean the size of each data segment. This is only possible with text read/write using Write #1, and Input #1.
Hope that helps.

Author Comment

ID: 1465168
I guess that would be a good way to do it if I was dealing with small
binary files. But, I am trying to  read 20 megabyte files, or bigger,one line at a time.
Just as it was written.
I just thought there might be a way to do it with regular Binary Open.
Why would they have that if you can't read the data back as you put it in?
Visual Online Books even sais that it's a good way to store binary data
in small files but that you have to read the data back sequentially.
They don't explain how to do it!
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Accepted Solution

alamo earned 400 total points
ID: 1465169
Hi, jgore. You have two options:

1) Add a delimiter after each record. VB has no builtin way to do this, so you need to read data back in chunks and reconstruct the records yourself. Since the data is binary you also need to look for the delimiter in your data and escape it.

Very clumsy. Much better:

2) When you write the data, prepend it with the length of the record to follow.

For example, write the file as:
 Dim RecLength as Integer
 RecLength = LenB(MyData1)
 Put #1,,RecLength
 Put #1,,MyData1

and read it as
 Get #1,,RecLength
 MyData1 = InputB(RecLength, 1)

If you are certain that your record lengths will be short you can save some filesize by making RecLength a byte rather than integer.

Hope this helps!

Author Comment

ID: 1465170
OK, that looks like it would work!
I'll try that and see if the file gets too big because I'm adding
the Length of each string as an integer.

I give you a "A" because it's the first real answer I've had yet that would work.
But, what does Visual Online Books mean when they say("Using Binary File Access"):
"The drawback to binary input/output with variable-length fields is that you can’t access records randomly — you must access records sequentially to learn the length of each record."
Do they mean that I have to write the Length per your example?
Which isn't so bad, but if there is a better way..................;-)

Expert Comment

ID: 1465171
I think Books Online meant to add "How to do this is left as an exercise for the reader." :-)

I have to assume they meant either of the methods I mentioned, or else writing a data type which is self-descriptive (my term for a variable which has a header which gives its length). I didn't mention self-descriptive data because it's more complicated to explain and boils down to the same thing as the example I gave. The more I think about it, though, it's at least worth mentioning.

The most useful example of self-descriptive data would be that of a variable length string. In Binary file mode VB doesn't write the header for a string, just the data. However if you write a user-defined type conatining a string, or write an array of strings, then VB is forced to write the header information. For example -

Dim MyData(0) as String
MyData(0) = "some data"
Put #1,,MyData

and then read with
Get #1,,MyData

Because MyData() is an array of variable-length strings (even though it only has one element) VB will write the string length before the data. As a result, the actual output file created will be identical to the file created by the method I described in my answer, VB just writes the data length for you. The drawback is this only works with a string, not with a byte array or any other sort of data.

If you want to write the data a litle more quickly, you can Dim the string array to more elements and write it all at once, but when you read it back you'd either need to read the strings one at a time or else properly Dim the array you are reading into. However, if you place an array inside a user-defined type or inside a variant then VB will write the entire array header. This adds to the file length but is useful if you don't know the dimensions of the array beforehand (i.e. you can Dim MyData() then Get #1,,MyData and it will figure out how many elements are in the array). It's also useful for arrays other than string arrays.

I hope I haven't confused you too much.. just ask if I haven't explained something well enough, it's late :-)

Expert Comment

ID: 4786619

Const Len = BytesToReadFromFile
Dim Var as string * Len
GET #f,bytePos,var

This read from file #f Len bytes from bytePos.
Note: Len must be equal less than 65535, so use two times at least to read from big files, such as yours.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Suggested Courses
Course of the Month13 days, 15 hours left to enroll

801 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