Re: reading a C++ structure from a binary file using C#



"Ben Voigt" <rbv@xxxxxxxxxxxxx> wrote in message news:eUJz4%23tcHHA.208@xxxxxxxxxxxxxxxxxxxxxxx

"Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message news:uklz82tcHHA.4260@xxxxxxxxxxxxxxxxxxxxxxx
Nand,

In order to do this in .NET, I would read from the file in 74 byte blocks (4 + 20 + 50). Then, I would call the static ToInt32 method on the

The C++ code used sizeof(s)... you must print this value out from C++. It is unlikely to be 74, since the structure contains an int member which prefers 4-byte alignment... so I think two bytes of padding is added by the C++ compiler and the record spacing will be 76 bytes. But check it, because it pragma pack was used several different values would be possible.


Why? The int is the first field of the struct, the second is a char array which has no alignment restriction, so, whatever the packing, the length written on disk will be 74.

Willy.

.