I have to write the following program and have no idea where to start. This is a binary file/image

1) Open an old file
2) Read until byte 56
3) Read the next 8 bytes to obtain the entire 9 digit cart frame
4) Keep reading byes until the HEX combination 49 49 2a 00 (HEX) is found.
5) When 49 49 2a 00 is found start writing an output file containing the bytes starting with 49 49 2a 00 For the output file use the cart-frame as the name of the file PLUS a number at the end to denote page number.
6) Keep reading and writing bytes to the output file UNTIL you reach end of file or you reach another 49 49 2a 00
7) If you hit another 49 49 2a 00 you will want to close the prior output file and open another output file. Increase the page number in the file name to denote the next page.
8) Repeat to process each old record

Does anyone have any suggestions on how to write this?

VB supports reading and writing to files using 'Open', 'Put' and 'Get' calls. Try this.

dim v as integer
dim offset as long
 
Open PFileName For Binary Access Read As #10
 
offset = 57
get #10, offset, v

Note that offset is in bytes. You can write other logic as necessary. If you need more help, you may ask me.

Here is a more complete and flexible example:

...
    OutputFileIndex = 0
    PageNum = 0
    ThisPage = ""
    InputFileIndex = FreeFile
    Open InputFileName For Binary Access Read As InputFileIndex
    CartNum = String(Chr(0), 8)                 ' This will make sure we grab 8 bytes.
    Get InputFileIndex, 56, CartNum             ' Get Len(CartNum) bytes from the file
                                                ' pointed to by InputFileIndex and
                                                ' starting at byte 56, and store them
                                                ' in CartNum.
    LastFour = String(Chr(0), 4)
    Do Until EOF(InputFileIndex)
        JustOne = Chr(0)
        Get InputFileIndex, , JustOne           ' Get Len(JustOne) bytes.
        LastFour = Mid(LastFour, 2) & JustOne   ' Keep track of the last four bytes
                                                ' we've acquired.
        If LastFour = Chr(&H49) & Chr(&H49) & Chr(&H2A) & Chr(&H00) Then
            If PageNum > 0 Then                 ' Don't write the extra info before
                                                ' the first page.
                OutputFileIndex = FreeFile
                Open "Cart " & CartNum & ".Page " & PageNum & "." & InputFileName For Binary Access Write As OutputFileIndex
                Put OutputFileIndex, , Mid(ThisPage, 1, Len(ThisPage) - 3)
                                                ' The last three bytes will be
                                                ' HEX 49 49 2A, which we don't want: 
                                                ' write all but the last three bytes.
                Close OutputFileIndex
                ThisPage = LastFour             ' Start a new page.
            End If
            PageNum = PageNum + 1
        Else
            ThisPage = ThisPage & JustOne       ' Add the byte to the current page.
        End If
    Loop
    Close                                       ' This will close *everything*, just
                                                ' in case.
...

You will undoubtedly have to tweak and otherwise modify this code to make it work properly with your own, but it gives the basic gist of what you're after. Hope this helps!

- Sendoshin

Comments
Good Work Here

Thanks for your help! I really appreciate this. I'll let you knwo if I get it to work.

Thanks Again:)

Ho do I grab 8 bytes as the file name? How do I check for four bytes in a row?

Private Sub Command1_Click()
Dim InputFileIndex As Integer
Dim OutputFileIndex As Integer
Dim Pagenum As Integer
Dim ThisPage As String
Dim strPath As String
Dim LastFour As String
Dim bytData As Byte
Dim CartNum As String

InputFileIndex = FreeFile

Dim InputFileName As String
Dim JustOne As String

InputFileName = "C:\Eastman Images\05070TYR1022478DS"
Open InputFileName For Binary Access Read As InputFileIndex

CartNum = String(0, 8) '''RETURNS NOTHING
Get InputFileIndex, 56, CartNum

'LastFour = String(Chr(0), 4) ''DOES NOT WORK
Do While Not EOF(InputFileIndex)
JustOne = Chr(0)
Get InputFileIndex, , JustOne
LastFour = Mid(LastFour, 2) & JustOne

If LastFour = Chr(&H49) & Chr(&H49) & Chr(&H2A) & Chr(&H0) Then
If Pagenum > 0 Then
OutputFileIndex = FreeFile
Open "cart " & CartNum & ".page " & Pagenum & "." & InputFileName For Binary Access Write As OutputFileIndex
Put OutputFileIndex, , Mid(ThisPage, 1, Len(ThisPage) - 3)

Close OutputFileIndex
ThisPage = LastFour
End If
Pagenum = Pagenum + 1
Else
ThisPage = ThisPage & JustOne
End If

Loop
Close

How do I grab 8 bytes as the file name?
...

CartNum = String(0, 8)  '''RETURNS NOTHING
Get InputFileIndex, 56, CartNum

First off, you're absolutely right. String(0, 8) DOES return nothing. Or rather, it returns nothing VISIBLE. In reality, CartNum becomes an 8-byte string filled with zeroes. If you were to look at the string in a hex editor, it would show "0000000000000000" - eight bytes of zeroes. The thing is, Chr(0) (the character whose ASCII code is 0) is completely invisible - it shows up as nothing. So what you end up with is 8 bytes of nothing. You could just as easily use 32 (space) or 65 (A) instead of 0; I personally prefer to use 0. However, the fact that it is 8 bytes long is what is important for the next step:

"Get", when used on a file opened For Binary, will return a number of bytes equal to the length of the string used as its third argument. What this means is that if CartNum is 8 bytes when Get is called, Get will put 8 bytes from the file into CartNum. If CartNum was 0 bytes long (the default Null value which all VB strings are initialized to), Get would return 0 bytes instead. So in the end, getting 8 bytes from a file requires setting up an 8-byte-long string (via String(0, 8) in this case), and then calling Get on a file opened For Binary. (A three step process!)

How do I check for four bytes in a row?
...

'LastFour = String(Chr(0), 4)  ''DOES NOT WORK
Do While Not EOF(InputFileIndex)
JustOne = Chr(0)
Get InputFileIndex, , JustOne
LastFour = Mid(LastFour, 2) & JustOne

If LastFour = Chr(&H49) & Chr(&H49) & Chr(&H2A) & Chr(&H0) Then
...

Here you caught a mistake I made. I was thinking that String() expected a string value as its first argument, when in reality it wants a number! The line should read LastFour = String(0, 4) . At any rate, things continue normally from there. We start a loop that will continue until we run out of data. Then we (re)set JustOne to an empty string one byte long, so that the Get line below it will pull out exactly one byte. Once Get does its thing (we left out the second argument to tell Get to continue where it left off), JustOne has the next byte in the file. The next step is to drop the first byte of LastFour (making it three bytes long) while simultaneously adding JustOne to the end (bringing it back up to four bytes). If we don't set LastFour to a 4-byte-long string (using the corrected version of the LastFour = ... line), the program will still try to drop the first byte from LastFour each time through, but since there isn't anything there for it to drop, the ultimate result will be that LastFour is only a single byte long - which is misleading and distinctly unhelpful.

As far as actually checking four bytes in a row, if LastFour is initialized correctly, the next line will check four bytes from the file for you. The &H inside each Chr() tells VB to read the numbers as hex values instead of decimal, so the If...Then will check for the four-byte sequence that separates each page. After that, things continue onward, splitting the old file into the new format.

Hopefully that helps, although I sense it's a bit late. ^_^;;

- Sen

This article has been dead for over six months. Start a new discussion instead.