469,607 Members | 1,889 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,607 developers. It's quick & easy.

Writing Structures To Binary Files

I am porting an application from C++ to C#, and am having trouble finding a way to quickly and efficiently write structures to a binary file. In C++ this is trivial because all that is necessary is to pack the structure to 1 byte boundries, and then just write out the structure directly to the File IO function

pragma pack (1

typedef struct
char var1[4]
int var1
}MyStruc

fwrite(&myStructure,sizeof(MyStruct),1,filepointer );
Things are clearly different in C# however(and .NET in general). I am using a FileStream(with a 4k buffer for maximum file performance) and it only writes byte arrays. So I think the only issue is how to efficiently turn a structure into a byte array

I know one can marshall the data into a byte array like so

[StructLayout(LayoutKind.Sequential, Pack = 1)
public struct MyStruc

[MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
public char[] var1;
public int var2
public class ByteManipulatio

/// <summary
/// Marshals data from an object to an array of bytes
/// </summary
/// <param name="obj">The object to be serialized</param
/// <returns>Array of bytes</returns
public static byte[] SerializeObjectToBytes(object obj

int size = Marshal.SizeOf( obj );
IntPtr buffer = Marshal.AllocHGlobal( size );
Marshal.StructureToPtr( obj, buffer, false );
byte[] data = new byte[ size ];
Marshal.Copy( buffer, data, 0, size );
Marshal.FreeHGlobal( buffer );
return data

but this has 2 problems for my application, 1 it is slow, and 2, it does not work because the Marshall.Sizeof() call fails because of the arrays in the structure (even with the MarshallAs attribute, it compiles fine but I get a runtime error

It would be preferable performance wise for me to use an unsafe block

unsaf

fixed(byte *pData = *(byte*)myStruct

but I keep getting an error that I cannot convert MyStruct to byte*.

Am I casting wrong in the unsafe block? There has to be a way to do this. All I want to do is get myStruct into a byte[]..

Also, I know one can use serialization to stuff objects into binary files, but I am unsure of the formatting effects of the serialization formatter. I need to control the format of the binary file - does the serialization add bytes to the file in any way that would confilct with a well defined file format

Thank you in advance for your help
Nov 16 '05 #1
2 7416
phyzics wrote:
unsafe
{
fixed(byte *pData = *(byte*)myStruct)
{

}
}

but I keep getting an error that I cannot convert MyStruct to byte*.

You need a pointer of myStruct first, to convert it to byte*. Try using

\\\
fixed(byte *pData = (byte*)&myStruct)
///
which would be the C++-way to do this but as I've never worked in C#
unsafe mode I can't say if that'll do it.

--
Konrad -
http://madrat.net/
Nov 16 '05 #2
Thanks for your reply, but this does not work:

the following code results in an error

unsafe
fixed(byte *pData = (byte*)&myStruct
that states that the right hand side cannot be a cast expression. Ok, so I'll try not typecasting..

unsafe
fixed(byte *pData = &myStruct
This generates an error that states that MyStruct cannot implicitly be converted to byte*

Any suggestions to go from here

----- Konrad L. M. Rudolph wrote: ----

phyzics wrote
unsaf

fixed(byte *pData = *(byte*)myStruct

but I keep getting an error that I cannot convert MyStruct to byte*.

You need a pointer of myStruct first, to convert it to byte*. Try usin

\\ fixed(byte *pData = (byte*)&myStruct
//
which would be the C++-way to do this but as I've never worked in C#
unsafe mode I can't say if that'll do it

--
Konrad
http://madrat.net/
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Sebastian Kemi | last post: by
5 posts views Thread by rob | last post: by
2 posts views Thread by DBC User | last post: by
7 posts views Thread by John Dann | last post: by
3 posts views Thread by nicolasg | last post: by
6 posts views Thread by arne.muller | last post: by
3 posts views Thread by =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.