Solved

reading a Binary file with header info

Posted on 2004-09-07
3
891 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

816 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