Solved

reading a Binary file with header info

Posted on 2004-09-07
3
897 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 86

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

733 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