Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading binary files into a struct?

Status
Not open for further replies.

Catthish

Programmer
Aug 18, 2003
1
US
I'm writing a Quake3 model loader in C#. I want to fill a struct with the file header data

Here's a tiny segment of the data :
Code:
	struct MD3Header
	{
		public int Ident;
		public int Version;
		public char[] Name;  // 64 chars long
	}


What's the best way of loading this from a file? Currently, I'm doing something like this:
Code:
	FileStream fileStream = File.OpenRead(filename);
	BinaryReader file = new BinaryReader(fileStream);
					
	MD3Header md3;
	md3.Ident = file.ReadInt32();
	md3.Version = file.ReadInt32();
	md3.Name = file.ReadChars(64);


Two questions:
Do I have to do it like this, specifying each individual element, or is there some way I can tell it to just stream the entire thing bit-for-bit into the struct?

If I can grab the entire lot in one go, how do I specify that (for instance) the Name element is actally 64 chars long? Given you can't specify array lengths within the struct declaration, it doesn't seem to be possible.

Thanks,
Cat
 
I'm at work, and don't have time to get you the details, but I think what you need to investigate is a ByteArray. I'm pretty sure you can read from a stream into one, and then go from there to a struct via one of the serialization methods.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top