Problem?

A

Ady

Hi,

I posted this a few days ago but it seems no one has replied any help much
appreciated.

I know that i have to serialise my class,

How could I pull out the value off offsetTextures and add it on to the
beginning of the file to get the offset to the start of my texture data.

typedef struct
{
float x0, y0, z0;
float x1, y1, z1;
float x2, y2, z2;
unsigned char r0,g0,b0;
} TRIANGLE;

typedef struct
{
char Id[4];
char Ver;
char header[90];
unsigned int offsetTextures;
TRIANGLE tri[];
} MyFile;

an example would be much appreciated.

Ady.
 
J

Jasper Kent

This looks like C++, not C#, so you may find more help in a different
newsgroup.

That said, what library are you getting your serialization class from?
MFC?

Regards,

Jasper Kent
 
A

Ady

No, i am trying to write a C# class that can handle loading in the data
based on that file structure, but cannot find a solution to pull out an
offset and add it onto the beggining of the file.

Thanks

Ady.
 
J

Jasper Kent

Can you write out your class examples in C#, to give a clearer idea of
the problem.

Thanks,

Jasper Kent
 
G

Greg Ewing [MVP]

Ady, that's definitely C++ and not C#. C# doesn't support typedef's. To
get you going in the right direction here's what I would try if I understand
what you are trying to do.

Create a structure and then use the StuctLayout attribute and FieldOffset
attribute to explicitly layout the in memory size and location of your
variables. You could also create an overlapping struct architecture so that
you push your string variable from your file into the WholeStruct variable
and then access the pieces using their positions. Here's an example C#
overlapping struct.

[StructLayout(LayoutKind.Explicit)]
public class OverlappingStruct
{
[FieldOffset(0) public string WholeStruct;
[FieldOffset(0)] public string Id;
[FieldOffset(10)] public string Name;
}

Hope that helps
 
M

Morten Wennevik

I think he means he has a file with triangle structs and a file format
header. He needs to get the offsetTextures number to find out where the
triangles end and the textures begin (I think).

Perhaps something involving System.IO.Stream stream =
System.IO.File.Open(file, System.IO.FileMode.Open);
byte[] buffer;
stream.Read(buffer, 95, 4);

I have absolutely no idea if this works, and it looks very ugly, but the
theory is that it should read 4 bytes starting from the 95th byte of the
file. This would be the offsetTextures number. There has to be a better
way of doing this.

Maybe something that fills a header structure (without TRIANGLE[]),
or something like
stream.Read(headerStruct, 0, sizeof(headerStruct);
 
A

Ady

Right, okay what I want to do is Load in a binary data file and reference
the data loaded in memory as ollows, i previously showed a c++ structure
just to show how the data is layed out not that i want to use c++, i would
prefer to do it in C# to keep everything consistent.

the problem is how would i load this data in and cast somekind of structure
to the binary data so that I could reference data with ease.

My other problem is pulling out the 4 Byte offset for (OFFSETTEXTURES) and
adding it onto the start of the file in memory to get access to my texture
data.

I hope this is a bit more clearer

Thanks

Adrian.

START OF FILE
---------------------------
4 BYTES ID
1 BYTE VER
90 BYTES HEADER
4 BYTES OFFSETTEXTURES ( RELATIVE OFFSET FROM START OF FILE)
UNKNOWN TRIANGLES[]

TEXTURES BEGIN

---------------------------

END OF FILE






----- Original Message -----
From: "Jasper Kent" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Monday, September 15, 2003 1:43 PM
Subject: Re: Problem?

Can you write out your class examples in C#, to give a clearer idea of
the problem.

Thanks,

Jasper Kent
for it!
 
G

Greg Ewing [MVP]

Ady, did you see my post about overlapping structs? You could use a Byte[]
which is the size of your struct and then have various other values
throughout to get the various data. Does that make sense? It would
basically allow you to 'force' a byte[] into a structure.

--
Greg Ewing [MVP]
http://www.citidc.com

Ady said:
Right, okay what I want to do is Load in a binary data file and reference
the data loaded in memory as ollows, i previously showed a c++ structure
just to show how the data is layed out not that i want to use c++, i would
prefer to do it in C# to keep everything consistent.

the problem is how would i load this data in and cast somekind of structure
to the binary data so that I could reference data with ease.

My other problem is pulling out the 4 Byte offset for (OFFSETTEXTURES) and
adding it onto the start of the file in memory to get access to my texture
data.

I hope this is a bit more clearer

Thanks

Adrian.

START OF FILE
---------------------------
4 BYTES ID
1 BYTE VER
90 BYTES HEADER
4 BYTES OFFSETTEXTURES ( RELATIVE OFFSET FROM START OF FILE)
UNKNOWN TRIANGLES[]

TEXTURES BEGIN

---------------------------

END OF FILE






----- Original Message -----
From: "Jasper Kent" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Monday, September 15, 2003 1:43 PM
Subject: Re: Problem?

Can you write out your class examples in C#, to give a clearer idea of
the problem.

Thanks,

Jasper Kent
for it!
 
J

Jay B. Harlow [MVP - Outlook]

Ady,
I posted this a few days ago but it seems no one has replied any help much
appreciated.
I have not abandon you, yet. ;-)

In .NET you will find the easiest way to do this is with Serialization.
Where you have an object graph and you read the entire graph in and write
the entire graph out.
I know that i have to serialise my class,
Serialization is not compatible with "pull out the value of offsetTextures
and add it on to the beginning of the file".
How could I pull out the value off offsetTextures and add it on to the
beginning of the file to get the offset to the start of my texture data.
You will need to use the System.IO.BinaryReader class along with the
System.IO.Stream class.

For starters you need to be certain that char is an 8 bit char or a 16 bit
Unicode Char. I'm treating it as an 8 bit C++ char (in other words a byte),
as your structures appear to be C++.

You can use BinaryReader something like (untested):

BinaryReader reader;
byte[] Id;
byte Ver;
byte header[];
uint offsetTextures;

Id = reader.ReadBytes(4);
Ver = reader.ReadByte();
header = reader.ReadBytes(90);
offsetTextures = reader.ReadUInt32();

reader.BaseStream.Seek(offsetTextures, SeekOrigin.Begin);
an example would be much appreciated.
See my previous post for examples & further information on BinaryReaders and
Serialization.

Hope this helps
Jay

Ady said:
Hi,

I posted this a few days ago but it seems no one has replied any help much
appreciated.

I know that i have to serialise my class,

How could I pull out the value off offsetTextures and add it on to the
beginning of the file to get the offset to the start of my texture data.

typedef struct
{
float x0, y0, z0;
float x1, y1, z1;
float x2, y2, z2;
unsigned char r0,g0,b0;
} TRIANGLE;

typedef struct
{
char Id[4];
char Ver;
char header[90];
unsigned int offsetTextures;
TRIANGLE tri[];
} MyFile;

an example would be much appreciated.

Ady.
 
A

Ady

Hi,

I understand your idea, and probably is best, thanks

Is there any documents on using these keywords on MSDN because I cant seem
to find any at all.
And when I compile I get errors

[StructLayout(LayoutKind.Explicit)]
public class OverlappingStruct
{
[FieldOffset(0) public string WholeStruct;
[FieldOffset(0)] public string Id;
[FieldOffset(10)] public string Name;
}



Greg Ewing said:
Ady, did you see my post about overlapping structs? You could use a Byte[]
which is the size of your struct and then have various other values
throughout to get the various data. Does that make sense? It would
basically allow you to 'force' a byte[] into a structure.

--
Greg Ewing [MVP]
http://www.citidc.com

Ady said:
Right, okay what I want to do is Load in a binary data file and reference
the data loaded in memory as ollows, i previously showed a c++ structure
just to show how the data is layed out not that i want to use c++, i would
prefer to do it in C# to keep everything consistent.

the problem is how would i load this data in and cast somekind of structure
to the binary data so that I could reference data with ease.

My other problem is pulling out the 4 Byte offset for (OFFSETTEXTURES) and
adding it onto the start of the file in memory to get access to my texture
data.

I hope this is a bit more clearer

Thanks

Adrian.

START OF FILE
---------------------------
4 BYTES ID
1 BYTE VER
90 BYTES HEADER
4 BYTES OFFSETTEXTURES ( RELATIVE OFFSET FROM START OF FILE)
UNKNOWN TRIANGLES[]

TEXTURES BEGIN

---------------------------

END OF FILE






----- Original Message -----
From: "Jasper Kent" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Monday, September 15, 2003 1:43 PM
Subject: Re: Problem?

Can you write out your class examples in C#, to give a clearer idea of
the problem.

Thanks,

Jasper Kent
for it!
 
G

Greg Ewing [MVP]

Yes, they are on MSDN. You can also find samples using www.google.com or
www.gotdotnet.com.

What errors do you get? I just typed that in from memory so there could be
a typo in there. I just compiled it and found I was missing a ]. Hopefully
you figured that out already. Also, don't forget to add the necessary using
statement.

using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Explicit)]

public class OverlappingStruct

{

[FieldOffset(0)] public string WholeStruct;

[FieldOffset(0)] public string Id;

[FieldOffset(10)] public string Name;

}


--
Greg Ewing [MVP]
http://www.citidc.com

Ady said:
Hi,

I understand your idea, and probably is best, thanks

Is there any documents on using these keywords on MSDN because I cant seem
to find any at all.
And when I compile I get errors

[StructLayout(LayoutKind.Explicit)]
public class OverlappingStruct
{
[FieldOffset(0) public string WholeStruct;
[FieldOffset(0)] public string Id;
[FieldOffset(10)] public string Name;
}



Greg Ewing said:
Ady, did you see my post about overlapping structs? You could use a Byte[]
which is the size of your struct and then have various other values
throughout to get the various data. Does that make sense? It would
basically allow you to 'force' a byte[] into a structure.

--
Greg Ewing [MVP]
http://www.citidc.com

Ady said:
Right, okay what I want to do is Load in a binary data file and reference
the data loaded in memory as ollows, i previously showed a c++ structure
just to show how the data is layed out not that i want to use c++, i would
prefer to do it in C# to keep everything consistent.

the problem is how would i load this data in and cast somekind of structure
to the binary data so that I could reference data with ease.

My other problem is pulling out the 4 Byte offset for (OFFSETTEXTURES) and
adding it onto the start of the file in memory to get access to my texture
data.

I hope this is a bit more clearer

Thanks

Adrian.

START OF FILE
---------------------------
4 BYTES ID
1 BYTE VER
90 BYTES HEADER
4 BYTES OFFSETTEXTURES ( RELATIVE OFFSET FROM START OF FILE)
UNKNOWN TRIANGLES[]

TEXTURES BEGIN

---------------------------

END OF FILE






----- Original Message -----
From: "Jasper Kent" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Monday, September 15, 2003 1:43 PM
Subject: Re: Problem?


Can you write out your class examples in C#, to give a clearer idea of
the problem.

Thanks,

Jasper Kent


for it!
 

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

Top