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



"Ben Voigt" <rbv@xxxxxxxxxxxxx> wrote in message news:ee$9PDwcHHA.1000@xxxxxxxxxxxxxxxxxxxxxxx

"Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx> wrote in message news:eDerzTucHHA.4632@xxxxxxxxxxxxxxxxxxxxxxx
"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.

As I said, the OP's C code writes sizeof(s) bytes to disk. Padding is included in sizeof, because sizeof is the separation between adjacent array elements which must both be properly aligned.

If there's only one record in the file, it's a non-issue. But I was responding to a post mentioning "read the file in 74 byte blocks".


Oh, I see what you mean, but here the sizeof depends on the packing specified (or the default packing). This is why I'm always trying to define my structs like this;


#pragma pack(show)
struct student
{
int roll_no;
char name[20];
char qualification[50];
};
#pragma pack(pop)
#pragma pack(show)

when writing to disk, especially when these files have to be read by "foreign" applications. No surprises here, the size is exactly the sum of all it's elements.

Willy.


.



Relevant Pages

  • [EXPL] Samba reply_nttrans() Remote Root Exploit
    ... struct variable_data_header ... int reserved2, paramcount, paramoffset, datacount, dataoffset, fid; ... extern int optind, errno, h_errno; ... memcpy(ptr + sizeof (struct tconx_request_header), data, bytecount); ...
    (Securiteam)
  • Re: reading a C++ structure from a binary file using C#
    ... because it pragma pack was used several different values would be possible. ... 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. ... Padding is included in sizeof, because sizeof is the separation between adjacent array elements which must both be properly aligned. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: character arrays, strings, printf and sendto
    ... I think the int is inadvertently null terminating the string and that is why ... But, when you set the char value, it is a one-byte ... > Maybe the problem is in struct member misalignment. ... You should use #pragma ...
    (microsoft.public.vc.mfc)
  • Re: Why does #pragma pack fail to function?
    ... int a:1; ... #pragma pack() ... I'm pretty sure the default one doesn't understand #pragma ... I've test the struct with gcc on my Debian lenny box and Ubuntu dapper box. ...
    (microsoft.public.vc.language)
  • [PATCH 2/2] ch: remove forward declarations
    ... static const struct { ... int errno, retries = 0, timeout, result; ... if (copy_from_user(&pos, argp, sizeof (pos))) ... unsigned int elem; ...
    (Linux-Kernel)