Runtime_error
Active member
- Joined
- Aug 5, 2003
- Messages
- 38
Hi I need some advice and possibly some help as well with Writing Binary data to files.
I have to output the data to a file in binary format into a predefined structure (not the programming Struct ). So basically the data in the file is organised in a known format.
File Header
Main Contents
.....
Please have a look at the following Code
If i pass the struct into the above function and then write the returned bytes to a file using the binarywriter then Will i have just the data from the structure ? or will a lot of .net stuff be written into the file as well.
Like i said the requirement is simply to store the data in the file not the structure of the STRUCT. Thanks
I have to output the data to a file in binary format into a predefined structure (not the programming Struct ). So basically the data in the file is organised in a known format.
File Header
Main Contents
.....
Please have a look at the following Code
Code:
[StructLayout(LayoutKind.Sequential)]
struct SomeFileHeader
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 4)]
public string Signature;
public Int32 Offset;
public Int32 Length;
}
public static byte[] RawSerialize( object anything )
{
int rawsize = Marshal.SizeOf( anything );
IntPtr buffer = Marshal.AllocHGlobal( rawsize );
Marshal.StructureToPtr( anything, buffer, false );
byte[] rawdatas = new byte[ rawsize ];
Marshal.Copy( buffer, rawdatas, 0, rawsize );
Marshal.FreeHGlobal( buffer );
return rawdatas;
}
If i pass the struct into the above function and then write the returned bytes to a file using the binarywriter then Will i have just the data from the structure ? or will a lot of .net stuff be written into the file as well.
Like i said the requirement is simply to store the data in the file not the structure of the STRUCT. Thanks
Last edited by a moderator: