Solved

reading a Binary file with header info

Posted on 2004-09-07
3
890 Views
Last Modified: 2012-06-21
I am trying to read a binary (HPGL/2) file that has some header info.

I need to be able to read the header info and know where it starts and ends. It always starts with "BGMF" and ends with "ENDMF;"
here is a bit of the code

dim strBuf as string

Open strFile For Input As #intFreeFile
        Input #intFreeFile, strBuf

If I do this I get everything from one comma "," to the next. unfortunaly the header info uses semicolons ";" to denote then end of any statement.
if I use:
Line Input #intfreefile, strBuf
Then I get the whole file becasue there are no chracter returns in the header info.

what I would really like is to get one statement at at time seperated by Semicolons ";" or an 'Input' command that buffer by something other than commas

Any Ideas?

BTW - I am using VB6 and my ultimate goal here is to strip out all the Header info
Thanks
0
Comment
Question by:Thrasymachus
3 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 12000396
Here is how you would split the file into two parts: the header and the rest of the file.

I'm not quite sure what you want to do after that...

By "my ultimate goal here is to strip out all the Header info" did you mean you want to write the rest of the file back to the original without the header info, or are you wanting to do something with the values in the header itself?

Idle_Mind

Option Explicit

Private Sub Command1_Click()
    Dim filename As String
    Dim entireFile As String
    Dim headerEnd As String
    Dim headerEndPos As Integer
    Dim header As String
    Dim restOfFile As String
   
    headerEnd = "ENDMF;"
    filename = "someFile.txt"
    If Dir(filename) <> "" Then
        Open filename For Binary Access Read As #1
        entireFile = Input(LOF(1), 1)
        Close #1
   
        headerEndPos = InStr(entireFile, headerEnd)
        If headerEndPos > 0 Then
            header = Left(entireFile, headerEndPos - 1)
            restOfFile = Mid(entireFile, headerEndPos + Len(headerEnd))
           
            ' do something with header or restOfFile...
           
        Else
            MsgBox headerEnd, vbInformation, "String not Found"
        End If
    Else
        MsgBox filename, vbCritical, "File not found"
    End If
End Sub
0
 
LVL 14

Expert Comment

by:aelatik
ID: 12000437
try something like this :

Dim intFreeFile As Integer
    intFreeFile = FreeFile

Dim Buffer As String
    Buffer = Space(FileLen(strFile))
   
Dim Header As String

Open strFile For Binary Access Read As #intFreeFile
    Get #intFreeFile, 1, Buffer
Close #intFreeFile

    Header = Mid(Buffer, InStr(1, Buffer, "BGMF", vbTextCompare) + 4, InStr(1, Buffer, "ENDMF;", vbTextCompare) - InStr(1, Buffer, "BGMF", vbTextCompare) + 4)
    For Each Item In Split(Header, ";")
    MsgBox Item
    Next
0
 
LVL 2

Author Comment

by:Thrasymachus
ID: 12000820
Thanks to Idle mind - That did what I needed it to. I was afraid that assigning the binary data to a string would not allow me to write it back to a file in the same format as it was from the start, But it seems to be working - Thanks for your help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

895 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

12 Experts available now in Live!

Get 1:1 Help Now