Solved

reading a Binary file with header info

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 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

18 Experts available now in Live!

Get 1:1 Help Now