Writing binary file

  • Thread starter Thread starter DBC User
  • Start date Start date
D

DBC User

Hi Sharpies,

I have a C program I am converting it into C#. Everything is fine
except this process creates a 6K byte binary file. This file initially
filled with 6K null and then start populating only the fields with
value in specified locations(3 seperate structures). The way the C does
is by creating a structures, which will be filled with null initially.
Then selectivly populating only the fields with values and only up to
the length of the string. The biggest structure is 2K in size.

What is the best approach to do this in c#?
 
DBC User,

I assume this also means that you are writing the structures through a
call to WriteFile, writing the structure directly to the file.

You could do this in .NET, I believe, if your structure is simple. By
simple, I mean that the structure has primitive types, embedded arrays in
it, no references to something outside the structure (basically, all the
information is contained in the memory of the structure). If this is the
case, then you can get a pointer to the structure in unsafe code, and then
pass that through interop to the WriteFile function through the P/Invoke
layer.

Hope this helps.
 
Back
Top