473,473 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reading Binary File in VS2005

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
Jan 9 '06 #1
6 1606
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.
"al jones" wrote:
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

Jan 9 '06 #2
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
Jan 10 '06 #3
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

On Mon, 9 Jan 2006 12:10:03 -0800, TrtnJohn wrote:
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.
"al jones" wrote:
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

Jan 10 '06 #4
On Mon, 9 Jan 2006 22:36:43 -0600, Darin Clark wrote:
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
Jan 10 '06 #5
On Mon, 9 Jan 2006 22:36:43 -0600, Darin Clark wrote:
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
Jan 11 '06 #6
On Mon, 09 Jan 2006 13:01:10 GMT, al jones wrote:
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
Jan 12 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
50
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.