|
-
March 23rd, 2004, 09:24 PM
#1
how to read a struct block from a file?
there is a Binary stream file from A/D ,its format is fixed by known struct ,like this:
{
int i1;
short i2;
float i3;
char[] i4; //size of array is 24
int i5;
...
int in;
}
i can read it with a struct p in c++ like this:
file.Read(&p,size(p);
you know, in c# ,it may be read with class Serialization method, but it will produce an error."...mscorlib.dll error...BinaryFormatter version incompatible...."
how can i read this struct in c#?
-
March 25th, 2004, 01:25 PM
#2
When you declare
char[] i4;
That means compiler can not know what size of array, so it could not read a block. Your struct is an managed code. So you must code it yourself.
-
March 28th, 2004, 09:57 PM
#3
thx!
can you give me an example?
-
March 29th, 2004, 06:56 AM
#4
Instead of define a struct, you define a class in C# and implement the ISerializable interface for that class. So anytime when you serializing or deserializing, you just need to call only one line of code.
Example :
[Serializable]
public class AAA : ISerializable
{
public int nAAA = 0;
public byte[] caGiday;
public int nBBB = 0;
public AAA()
{
}
public AAA(SerializationInfo info, StreamingContext context)
{
nAAA = info.GetInt32("nAAA");
caGiday = new byte[24];
for (int i = 0; i < 24; i++)
caGiday[i] = info.GetByte("caGiday"+i.ToString());
nBBB = info.GetInt32("nBBB");
}
public void GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.AddValue("nAAA", nAAA);
for (int i = 0; i < 24; i++)
info.AddValue("caGiday"+i.ToString(), caGiday[i]);
info.AddValue("nBBB", nBBB);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|