Computing.Net > Forums > Programming > VB6 Read/Write Binary File

Computing.Net: Over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to sign up now, it's free!

VB6 Read/Write Binary File

Reply to Message Icon

Original Message
Name: Michael
Date: June 10, 2002 at 15:49:33 Pacific
Subject: VB6 Read/Write Binary File
Comment:

How would I open a Binary file and read a certain byte, i.e. byte 11?

Could I then display this byte in a textbox as a hex number?

Also how would I write a certain byte?

Look forward to your reply!

Michael


Report Offensive Message For Removal


Response Number 1
Name: Jeff J
Date: June 10, 2002 at 20:52:33 Pacific
Reply: (edit)

For better or worse, the easy-to-use FileSystemObject class cannot access in binary mode, only in text mode. Then again, that's probably a good thing, since the FSO is part of VBScript, and hence is inefficient. You need to use the old-style VB I/O statements, which are a little odd looking, but really work a lot like typical C I/O. For example:


Dim iFile As Integer
Dim lByteLen As Long
Dim bytData As Byte
Dim bytArray() As Byte
Dim sHex As String

'get an available file ID
iFile = FreeFile

'open the file for access
Open "C:\MyFile.ext" For Binary As iFile

'get current len of file
lByteLen = LOF(iFile)

If lByteLen >= 11 Then
  Get iFile, 11, bytData
  sHex = Hex(bytData)

'write over a byte
  bytData = Asc("Z")
  Put iFile, 9, bytData

'grab entire file contents
  Redim bytArray(1 To lByteLen)
  Get iFile, 1, bytArray
End If

'must close file when done
Close iFile

TextBox1.Text = sHex

It's important when accessing in binary mode, that you check how many bytes exist before attempting to read at a specific byte position. Also, note that VB uses 1-based arrays, so the first byte is #1, not zero as in C and most other languages. The byte position for Get and Put (2nd parameter) indicates where reading/writing begins, but not the number of bytes accessed. VB figures that out automatically for you, depending on the size of the variable to be loaded/read (3rd param). By resizing bytArray to the size of the file, Get() was able to load the entire file into the array.

Converting to hex is as simple as calling the Hex() function, as shown.

For more options with "Open", put your cursor on it, and hit F1 to invoke the corresponding help file.

Say, it's been so long since I've used these routines, that I did a quick check on MSDN to see if I screwed-up. There I found a page I wish was around in the VB3 days:

"http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon98/html/vbconmanipulatingfiles.asp"

I think you'll find it a good overview of VB file I/O, though it has no examples of binary access.

Cheers


Report Offensive Follow Up For Removal

Response Number 2
Name: John W. Borelli
Date: June 10, 2002 at 21:02:34 Pacific
Reply: (edit)

Jeffs input is absolutely correct with one correction. As he pointed out, VBs file pointers start the file at offset 1 (one). Therefore the code snippet that reads:

If lByteLen >= 11 Then
Get iFile, 11, bytData
sHex = Hex(bytData)

should read:

If lByteLen >= 12 Then
Get iFile, 12, bytData
sHex = Hex(bytData)

otherwise you will be accessing/reading/writing byte 10 not byte 11, otherwise this will work for you well.

borelli33


Report Offensive Follow Up For Removal

Response Number 3
Name: Jim
Date: June 10, 2002 at 21:10:47 Pacific
Reply: (edit)

Alternately, you can use standard Win32 APIs, CreateFile, SetFilePosition, ReadFile or WriteFile, and CloseHandle. If you're only reading or writing a byte at a time, it's pretty easy. You don't have to worry about VB's confusing way of storing chunks of memory.


Report Offensive Follow Up For Removal

Response Number 4
Name: Jeff J
Date: June 11, 2002 at 00:06:41 Pacific
Reply: (edit)

Actually I was assuming Michael was thinking in VB's 1-based terms, which is why I used 11. But John is technically correct, that the computing world always counts bytes from zero, so perhaps I shouldn't have been so presumptuous.

Fortunately, Micro$oft has stopped insisting that 1-based is easier (this post is a testament to that fallacy), and starting with VB7, everything is now zero-based by default. As if we'll ever have harmony in the programming world...


Report Offensive Follow Up For Removal

Response Number 5
Name: Michael
Date: June 11, 2002 at 00:43:11 Pacific
Reply: (edit)

Thanyou for your help guys!

All sounds easy, btw this is the firt programming I have attempted since BBC Basic, very scary!


Report Offensive Follow Up For Removal







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home








Do you have your own blog?

Yes
No
I did before
I will soon


View Results

Poll Finishes In 4 Days.
Discuss in The Lounge
Poll History




Data Recovery Software