Solved

Binary File Input/Output.

Posted on 1998-07-07
7
364 Views
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
0
Comment
Question by:jgore
7 Comments
 
LVL 1

Expert Comment

by:khaledc
Comment Utility
Question is already been posted
0
 
LVL 7

Expert Comment

by:kamall
Comment Utility
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.
Regards
0
 

Author Comment

by:jgore
Comment Utility
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!
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Accepted Solution

by:
alamo earned 100 total points
Comment Utility
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!
0
 

Author Comment

by:jgore
Comment Utility
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..................;-)
0
 
LVL 6

Expert Comment

by:alamo
Comment Utility
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 :-)
0
 

Expert Comment

by:binary208
Comment Utility
Use:

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.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now