Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Binary files, 1

Status
Not open for further replies.

thepunisher

Programmer
Jul 23, 2004
108
IE
Hi,

I want to read a binary file and store each byte/character into an array.

Then i want to check what each of the bytes are, whether they are a 1, a 2 etc.. based on this, i will do some other computations.

The main thing i want to do is read/open a binary file, and put each byte into an array,

How is this done?

I have tried a few things, such as GET and PUT, and open and write, they dont do wot i want,

thanks, in advance.

thePunisher!


Homer Simpson: Im not a religious man, but Superman, if you're up there, save me!!!
 

Here is one way:
Code:
Option Explicit
Dim FileBytes(1000) As Byte

Sub test()
Dim nLength As Long
Dim i As Long
Dim s As String
  nLength = BinaryFile("c:\test.bin")
  For i = 1 To nLength
    s = s & " " & FileBytes(i)
  Next i
  MsgBox s
End Sub

Function BinaryFile(AFileName As String) As Long
Dim AByte As Byte
Dim nRecNum As Long
  Open AFileName For Random As #1 Len = 1
  nRecNum = 1
  While Not EOF(1)
    Get #1, nRecNum, AByte
    FileBytes(nRecNum) = AByte
    nRecNum = nRecNum + 1
  Wend
  Close #1
  BinaryFile = nRecNum - 2
End Function
 
Legend, i'll try it and let you know in the morning how i get on thanks,

Have a star!!

Homer Simpson: Im not a religious man, but Superman, if you're up there, save me!!!
 
I have tried the code above,

but it keeps coming up with a 'Run-Time Error:9 'Subscript out of range' error message,

Whats the story?

Does this mean that the array:

Dim FileBytes(1000) As Byte

Has to be bigger?


"Cellar Door" - the most beautiful word in the english dictionary
 
I have increased the size of the array, but thats not the problem.

It comes up with the same error as above, at the line:

FileBytes(nRecNum) = AByte

At run-time, when i step through the code nRecNum has a value of 1001 while AByte is 32, whats the problem?

Please help

"Cellar Door" - the most beautiful word in the english dictionary
 
I take it you don't have too much coding experience in VB/VBA. Otherwise you would have noticed the above code is not complete and that you would have to modify it to fit your needs.

Most of us here will not simply give out complete coding solutions (as you found out). You, too, have to make an effort. Since you didn't show any of your own code, Zathras simply tried to give you an idea as to how you might be able to accomplish your goal.

If you posted some of your code, which would have shown us that you are making an attempt, we would have explained any errors we found or workarounds to your problem.

Here's a hint at the area(s) you should pay closer attention to (based on Zathra's code above):


[red]
While Not EOF(1)
[/red]

--AND you need to realize--

[red]
FileBytes(x)
[/red]



As for "AByte = 32", it simply means that AByte is currently containing a space. So don't get upset when "...nRecNum has a value of 1001 while AByte is 32..." The ASCII character code for a "space" is 32 and it has nothing to do with the error you are currently getting.

If you are still at a loss, ask specificly what you don't understand.

--MiggyD
 
This is the code i had been trying.

I also was trying to read in a text file and create another binary output file and text file.

As you can see by the code i had been using, i was a million miles away.

-----------------------------------------------------
Dim x As Integer
Dim y As Integer
Dim z As Integer

Const ROW_SIZE = 14
Const COLUMN_SIZE = 44
Dim sTemp As String

'Set the file names of the input files:
inFileName1 = worksheetInputFile1
inFileName2 = worksheetInputFile2

'Set the file names of the output files:
outFileName3 = worksheetOutputFile1
outFileName4 = worksheetOutputFile2

inFileNo1 = 1 'Set the file number to 1.
inFileNo2 = 2 'Set the file number to 2.
outFileNo3 = 3 'Set the file number to 3.
outFileNo4 = 4 'Set the file number to 4.

Open inFileName1 For Input As inFileNo1 'Open input files.
Open inFileName2 For Input As inFileNo2
Open outFileName3 For Append As outFileNo3 'Open output files for appending/writing.
Open outFileName4 For Append As outFileNo4

'Loop through all the records.
Do While Not EOF(1) 'Do until it is the end of the file.

Line Input #inFileNo1, sTemp$

Write #outFileNo4, , "HELLO" 'Testing if text can be written to report file.

Write #outFileNo4, sTemp$ 'Write to text file.
Write #outFileNo3, sTemp$

Loop

Close inFileNo1 'Closes input files.
Close inFileNo2

Close outFileNo3 'Closes output files.
Close outFileNo4

Application.StatusBar = "Files Closed and saving workbook"
ActiveWorkbook.Save 'Save workbook.
Application.Quit 'Close down Microsoft Excel.

End Sub

"Cellar Door" - the most beautiful word in the english dictionary
 
What does the "While Not EOF(1)"

mean exactly?

"Cellar Door" - the most beautiful word in the english dictionary
 
I know it means while it is not the End Of File loop, but why is the (1) there?

"Cellar Door" - the most beautiful word in the english dictionary
 
But with regards to the code you guys gave me, this is what i have done so far:

Dim FileBytes(10000) As Byte 'An array of bytes.

-----------------------------------------------------------
Sub test()
Dim nLength As Long
Dim i As Long
Dim s As String

'Length of the file in bytes.
nLength = BinaryFile("C:\David McGarry\Week14\27 September 2004\Modloads\inDAVE.BIN")

For i = 1 To nLength
s = s & " " & FileBytes(i)
Next i
MsgBox s
End Sub
-----------------------------------------------------------
Function BinaryFile(AFileName As String) As Long

'Declarations.
Dim AByte As Byte
Dim nRecNum As Long

'Open the stated binary file. File is opened for Random access.
'Len is the number of bytes required to store a variable.
Open AFileName For Random As #1 Len = 1
nRecNum = 1
While Not EOF(1) 'While it is not the end of the file.
Get #1, nRecNum, AByte
FileBytes(nRecNum) = AByte

nRecNum = nRecNum + 1 'Increment record number.
Wend

Close #1
BinaryFile = nRecNum - 2

End Function
-----------------------------------------------------------

Why am i getting the previous error? as i asked above, as i dont really understand where it comes from!

thanks

"Cellar Door" - the most beautiful word in the english dictionary
 
Code:
  For i = 1 To nLength
    s = s & " " & FileBytes(i)
  Next i
  MsgBox s
was only for demonstration purposes. With a large file it will be difficult to stuff all of that in to a string and show it with a MsgBox.

How big is inDAVE.BIN? You have correctly identified the problem as the array is too small. That is what 'Subscript out of range' is trying to tell you. Even though you have increased the array size to 10,000 bytes, I would guess that your file is larger than that.

I answered your question, but you probably asked the wrong question. What is it exactly that you are trying to do?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top