VBA
Discuss AutoCAD ActiveX and VBA (Visual Basic for Applications) questions here.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Reply
Message 1 of 2
Anonymous
227 Views, 1 Reply

vb get

I haev an old binary file. In VBA, I use "Get #i, 23, MyString" to get my
string MyString from file #i, and MyString is located at 23.

In vb how do i translate "Get #i, 23, MyString"
1 REPLY 1
Message 2 of 2
rocheey
in reply to: Anonymous

>>I haev an old binary file.
>>In vb how do i translate "Get #i, 23, MyString"

ask an old vb programmer. Maybe one who wrote his first "Hello, World" program on papyrus 😕

Just kidding; actually, reading/writing binary files is blazingly fast, and can add a layer of security to your files because it might scare -some- people off from modifying it when they see all those strange characters.

You use "Get" to read from a binary file, and "Put" to write to a binary file.

"#i" is stating which open file you are working with. While its normally "#1", because you normally work with one at a time, its good programming practice to use a variable, like "i", in its place.

The "23" is the offset into the file. In this case, it is the 23rd byte. You start counting here at 1, not zero, in case you wanna look at the file in notepad, and count them down yourself.

"MyString" is the variable that the data will be read into. MyString looks to be a string variable.

However, in this case, there is information missing. When reading a binary file, you have to tell VB how many bytes to read in, at each time.
If you had declared an integer named "MyInt", and used "Get #i, 23, MyInt" , VB would know to go to the 23rd byte, and read -2 bytes-, and return the value of those 2 bytes, as an integer, in the variable "MyInt". If you had declared a Long Int, "Dim MyLng as Long", then VB would know to read 4 bytes, because a long int is 4 bytes long. And if you declared "Dim MyDbl as double", then used "Get #i, 47, MyDbl", then VB would know to go to the 47th byte, and read in 8 bytes, because a double is 8 bytes long.

String variables, however, can be of almost any length. So what is usually done is to declare a string up front, and give it a length, in a few different ways:

Dim MyString as String * 12 ' declare a string 12 bytes long
- or -
MyString = String$(10," ") ' declare a string 10 bytes long
- or -
MyString = Space$(64," ") ' decalre a string 64 bytes long.

Then, when you ask VB to go to byte offset 23, and get a string variable, it knows HOW MANY characters to get.

' here is some sample code to first write, and then read, info from a binary file. It uses strings, Integers, and doubles as variables,
' and, since we are writing the file ourselves, we know the format, and the location of each variable, so we wont NEED to use byte offsets.
' we can just make the byte offset parameter EMPTY, and it will read/write from wherever we last did a read or write. It will use the
' current end of the file....

Sub BinaryTest()
' declare our path/file name to write and read
Dim MyFile As String: MyFile = "C:\TestFile.bin"

' declare some variables to write/read as binary access
Dim MyInt As Integer: MyInt = 21
Dim MyDbl As Double: MyDbl = 3.14159

Dim MyString As String * 32: MyString = "This is a test"
' Now, above, i declared the string as 32 bytes long, but only used 14 bytes.
' The file will be written as 32 bytes long.


' use the "FreeFile" property to get the next file handle
Dim I As Integer: I = FreeFile

' Open the file as binary: if it doesnt exist, it will be created for us.
Open MyFile For Binary As #I

' write our variables to disk in binary

' write the integer
Put #I, , MyInt ' since no byte offset given use the last place we wrote to (beginning)
' OR, if you want to do things the hard way:
'Put #I, 1, MyInt 'write directly to byte offset 1

' write the double
Put #I, , MyDbl ' since no byte offset given use the last place we wrote to (after the Int)
' OR, if you want to do things the hard way:
'Put #I, 3, MyDbl 'write directly to byte offset 3: Bytes 1-2 are the integer

' write the string
Put #I, , MyString ' since no byte offset given use the last place we wrote to (after the Double)
' OR, if you want to do things the hard way:
'Put #I, 11, MyString 'write directly to byte offset 11: Bytes 3-10 are the Double

' Now we'll close the file, clear the variables, and read the values back in
Close #I
MyInt = 0: MyDbl = 0#: MyString = Space$(32)

' we'll get the file handle again
I = FreeFile

' we use the same call to read or write in binary; you can do both at the same time, if you wish!
Open MyFile For Binary As #I

' we'll read the data back in:

' read the integer
Get #I, , MyInt ' since no byte offset given use the last place we read from (beginning)
' OR, if you want to do things the hard way:
'get #I, 1, MyInt 'read directly to byte offset 1
Debug.Print "MyInt="; MyInt ' output to debug window

' read the double
Get #I, , MyDbl ' since no byte offset given use the last place we read from (after the Int)
' OR, if you want to do things the hard way:
'Get #I, 3, MyDbl 'read directly to byte offset 3: Bytes 1-2 are the integer
Debug.Print "MyDbl="; MyDbl ' output to debug window

' read the string
Get #I, , MyString ' since no byte offset given use the last place we read from (after the Double)
' OR, if you want to do things the hard way:
'Get #I, 11, MyString 'write directly to byte offset 11: Bytes 3-10 are the Double
Debug.Print "MyString="; MyString ' output to debug window

'And close the file again
Close #I


End Sub

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost