Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 909
  • Last Modified:

reading a Binary file with header info

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
Thrasymachus
Asked:
Thrasymachus
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
aelatikCommented:
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
 
ThrasymachusAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now