Binary I/O and structures

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a simple structure that should be 12 bytes long (3 integers). When I
use
fileput(1,astructure), I discover that 24 bytes have been written. If you
look at the structure in debug you see that it has a system.value object. It
looks like the fileput is writing this info (which I'm guessing is 12bytes
long) as well as the data. Is there some way to stop this behaviour and make
it only write the data?
 
rossu said:
I have a simple structure that should be 12 bytes long (3 integers).
When I use
fileput(1,astructure), I discover that 24 bytes have been written.
If you look at the structure in debug you see that it has a
system.value object. It looks like the fileput is writing this info
(which I'm guessing is 12bytes long) as well as the data. Is there
some way to stop this behaviour and make it only write the data?

FilePut ugh, it's hardly .NET is it?

..NET is allowed to represent structures in memory any way that it likes,
so I suspect that this is the problem. There is a way to get round it.
You can put a [StructLayout] attribute on it to specify the memory
layout that you want. Use [StructLayout(LayoutKind.Sequential)] if you
use types of the exact size that you expect in the file. You can also
use LayoutKind.Explicit if you want to specify the size.

Richard
 

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

Back
Top