Reading into a structure.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I guess I am simply blind this morning. I have a binary file I want to read into a predefined structure of ints/strings/shorts and arrays. How do I go about this. In C++ I would simply define a type and read into it. All I can find now is strings, chars and bytes.
 
You can still create a structure and read data into it, object-wise. I'm
often using this approach.
C# can also serialize an object (for instance a structure) and write/read
directly, but the performance is extremely low and memory and disk consuming
(but its VERY easy).

/Morten Nielsen
http://www.iter.dk

BobTheHacker said:
I guess I am simply blind this morning. I have a binary file I want to
read into a predefined structure of ints/strings/shorts and arrays. How do
I go about this. In C++ I would simply define a type and read into it. All
I can find now is strings, chars and bytes.
 
I have tried type casting back, but must just be missing something. Must
be the Cold Medicine Kickiing in!

Don't type-cast your structure, but type-cast the elements in the structure
instead.
If that is what you want, you need to make your structure serializable and
use the deserializer to read the data (there is a nice example in the SDK).
But I don't think you can deserialize a structure that has been written to a
file from C++.

/Morten
 
I have stumbled in the formatter stuff and it mentions serialize/deserialize

What is the name of the Example. I haven't hit it yet. Below is the cut of my Structure. The file is in Binary format. This all seems to be a weak spot in MSDN. I must just be hitting all around it. I've tried this and get errors

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()

tr

V2Style sV2Style = (V2Style)bFormatter.Deserialize(fs)

catch(Exception f

MessageBox.Show(f.Message.ToString())


[ StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)
public struct V2Styl

[ MarshalAs(UnmanagedType.ByValTStr, SizeConst=33) ] public string szTagname
[ MarshalAs(UnmanagedType.ByValTStr, SizeConst=33) ] public string pTagContext
public short font
public short emphasis
public short background
public short foreground
public short size
public short LineSpace
public short SpaceAbove
public short SpaceBelow
public short IndentLeft
public short IndentRight
public short IndentFirst
[ MarshalAs(UnmanagedType.ByValArray, SizeConst=10) ] public short tabsArray
public short justification
public ushort Flags
[ MarshalAs(UnmanagedType.ByValTStr, SizeConst=82) ] public string AttributeList
[ MarshalAs(UnmanagedType.ByValTStr, SizeConst=32) ] public string facename
public uint RGBfore; /* Foreground text color - version 2.0 *
public uint RGBback; /* Background text color - version 2.0 *
public byte BorderStyle
[ MarshalAs(UnmanagedType.ByValTStr, SizeConst=5) ] public string extra
 
Back
Top