Reading Binary File in VS2005

A

al jones

I picked a good project to try to learn to use VS - and up till now
everything has worked the way I expect. The code is from a VS.net class,
which I picked up on the web, from which I've extracted portions to fulfill
my immediate needs.
I hsve the following:

Structure OFFSETTABLE ' (page 32: "The Table Directory")
Friend Version As Long ' signed floating point number
' 2.14 (0x00010000 for TTF version 1.0)
Friend NumberOfTables As Integer ' number of tables
Friend SearchRange As Integer ' maximum power of 2 <= numTables)
x 16
Friend EntrySelector As Integer ' Log2: maximum power of 2 <=
numTables)
Friend RangeShift As Integer ' NumTables * (16 - searchRange)
End Structure

Private Function RefreshFontInfo(ByVal FileNameTTF As String) As
Boolean
Dim TableOffsets As OFFSETTABLE

hFile = FreeFile()
FileOpen(hFile, FileNameTTF, OpenMode.Binary, OpenAccess.Read,
OpenShare.LockReadWrite)
'Open FileNameTTF For Binary Access Read Lock Write As #hFile

' Get the offset table
FileGet(hFile, TableOffsets, Len(TableOffsets)) <<----

Two questions here:
1) the marked line appear to *NOT* read anything, I know what the values
from the file should be - the resulting structure contains zero's. What am
I doing wrong?
2) What's the easiest way to throw up a generica error handler - giving
error number and message if this isn't doing what I think it is?

The broken lines are a resuilt of the newreader message box, all logical
<?> lines are contiguous.

Thanks for any assistance //al
 
G

Guest

If you look up user defined data-types in the help file it will say:

"Note: As with all composite data types, you cannot safely calculate the
total memory consumption of a structure by adding together the nominal
storage allocations of its members. Furthermore, you cannot safely assume
that the order of storage in memory is the same as your order of declaration."

Because of this I would avoid trying to read an entire structure. You'll
need to read each element in one at a time.
 
D

Darin Clark

You may have gotten this code from a vs.net class but FreeFile, FileOpen and
Open are leftovers from a previous generation of coding.
You should be looking at FileStreams instead.

Dim FileStream as New IO.FileStream(FileName, IO.FileMode.OpenOrCreate,
IO.FileAccess.Read, IO.FileShare.ReadWrite, 8)

There are examples in the online help of the many options of filestreams and
how to use them.



Darin Clark
 
A

al jones

John, if you're telling me that I can't read a structure in VS2005 then I'd
be inclined to say that there is something seriously wrong with the
implementation. I'd rather think that it was interpretation.

I have another set of code which does read it properly - and I'm admitteldy
plagarizing code (in both cases *with* permission) to do what I want. What
I don't understand is why this code (which is much more concise) doesn't
appear to function the same way as the other (which I haven't posted) does
- obviously somewhere in my copying, I've blown something, but I don't see
it and was hoping that one of you all would.

And if my understanding of the structure storage in memory is correct, the
elements of the structure will be word aligned which causes the storage of
the structures elements to take up more space than the total of the size of
the elements. ((If I'm wrong on this, someone please correct me.))

((Please excuse the top posting, I'm just following the thread of the
message as he replied to it.))

//al
 
A

al jones

You may have gotten this code from a vs.net class but FreeFile, FileOpen and
Open are leftovers from a previous generation of coding.
You should be looking at FileStreams instead.

Dim FileStream as New IO.FileStream(FileName, IO.FileMode.OpenOrCreate,
IO.FileAccess.Read, IO.FileShare.ReadWrite, 8)

There are examples in the online help of the many options of filestreams and
how to use them.



Darin Clark

Thanks darin, I've been looking there - and at almost everything that I can
find that references reading a binary file.

Looking specifically at the example for the BinaryReader class - are you
saying that once I've created an instance of a structure that I then need
to read the individual elements of the structure using BinaryReader?? This
is what I'm understanding and really don't want to believe there isn't a
reasonalbe way to directly read the instance.

Like I said earlier, munging around in the tables that make up the header
of the ttf file format may not have been the best first project to bring up
in VS but I have programmed before, albeit many years ago, and am doing a
realy good job on my old brain.

Thanks //al
 
A

al jones

You may have gotten this code from a vs.net class but FreeFile, FileOpen and
Open are leftovers from a previous generation of coding.
You should be looking at FileStreams instead.

Dim FileStream as New IO.FileStream(FileName, IO.FileMode.OpenOrCreate,
IO.FileAccess.Read, IO.FileShare.ReadWrite, 8)

There are examples in the online help of the many options of filestreams and
how to use them.



Darin Clark

Okay Darin, I give up, how would you do this if it were your mess?? I read
and understand the words, but don't see how to implement the readers to
this situation. If you have the time and patience, could you give me an
example of how to 'read' data into a structure with the streamreaders. If
I were doing cobol from many moons ago, I could do something like
byte_array redefines ttf_struct (77 levels??) but I'm not and I don't
understand. Would you mind humouring me please? Thanks //al
 
A

al jones

I picked a good project to try to learn to use VS - and up till now
everything has worked the way I expect. The code is from a VS.net class,
which I picked up on the web, from which I've extracted portions to fulfill
my immediate needs.
I hsve the following:

Structure OFFSETTABLE ' (page 32: "The Table Directory")
Friend Version As Long ' signed floating point number
' 2.14 (0x00010000 for TTF version 1.0)
Friend NumberOfTables As Integer ' number of tables
Friend SearchRange As Integer ' maximum power of 2 <= numTables)
x 16
Friend EntrySelector As Integer ' Log2: maximum power of 2 <=
numTables)
Friend RangeShift As Integer ' NumTables * (16 - searchRange)
End Structure

Private Function RefreshFontInfo(ByVal FileNameTTF As String) As
Boolean
Dim TableOffsets As OFFSETTABLE

hFile = FreeFile()
FileOpen(hFile, FileNameTTF, OpenMode.Binary, OpenAccess.Read,
OpenShare.LockReadWrite)
'Open FileNameTTF For Binary Access Read Lock Write As #hFile

' Get the offset table
FileGet(hFile, TableOffsets, Len(TableOffsets)) <<----

Two questions here:
1) the marked line appear to *NOT* read anything, I know what the values
from the file should be - the resulting structure contains zero's. What am
I doing wrong?
2) What's the easiest way to throw up a generica error handler - giving
error number and message if this isn't doing what I think it is?

The broken lines are a resuilt of the newreader message box, all logical
<?> lines are contiguous.

Thanks for any assistance //al

Well, would anyone offer some practical guidance on how it should be done -
I've gone round the MSDN and don't understand how to read into a structure.
//al
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top