Writing Structures To Binary Files

G

Guest

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
 
K

Konrad L. M. Rudolph

phyzics said:
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.
 
G

Guest

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


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
 

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