TO all:

here ,I have a struct.

struct test{
char xx;
char *yy;
}

Now ,How can I write it into a binary file?

Re: How to write a binary file by David

David
Mon Apr 26 01:03:17 CDT 2004

On Mon, 26 Apr 2004 05:17:27 UTC, "terrcy" <terrcy@cn-finet.com> wrote:

> TO all:
>
> here ,I have a struct.
>
> struct test{
> char xx;
> char *yy;
> }
>
> Now ,How can I write it into a binary file?

Look at the file open command of the language/compiler
that you are using. That is where you generally declare
that the file mode is binary. Since there are any number
of ways to manage files in Windows, we'd need to know your
preferred interface to give specifics.

You have several options when writing the structure
to a file. You need to decide what your purpose is for
storing things in a file, who will be reading them,
and what compatibility you want to maintain.

Writing a pointer (char *yy) to a file will likely have
no meaning when you read it back. Perhaps your intent
is to write the value pointed to by yy to the file.
That would be an indication that yo don't want to just
write the direct contents of the test structure to the
file.

Another issue to consider is that of alignment. (char)
may be byte aligned and (char *) may be doubleword
aligned. The default alignment mode may insert dead space
into items like a structure to improve access performance.
When you intend to write items like structures to a file,
stream, or other byte oriented device, you may want to note
or remove this dead space. The #pragma align directive can
be used to change this behavior.

If your purpose will include reading this file back from
another system, you may also want to note what order the
bytes are written. In the case of multibyte objects, like
a pointer or doubleword, different systems may presume the
bytes are in a different order. This is important when
the communication medium would be an IP based method.

David

Re: How to write a binary file by terrcy

terrcy
Mon Apr 26 01:11:11 CDT 2004

ths
"David" <FlyLikeAnEagle@United.Com> wrote in message
news:rOdGr40LMPU3-pn2-H8fyVVFdf6lD@localhost...
> On Mon, 26 Apr 2004 05:17:27 UTC, "terrcy" <terrcy@cn-finet.com> wrote:
>
> > TO all:
> >
> > here ,I have a struct.
> >
> > struct test{
> > char xx;
> > char *yy;
> > }
> >
> > Now ,How can I write it into a binary file?
>
> Look at the file open command of the language/compiler
> that you are using. That is where you generally declare
> that the file mode is binary. Since there are any number
> of ways to manage files in Windows, we'd need to know your
> preferred interface to give specifics.
>
> You have several options when writing the structure
> to a file. You need to decide what your purpose is for
> storing things in a file, who will be reading them,
> and what compatibility you want to maintain.
>
> Writing a pointer (char *yy) to a file will likely have
> no meaning when you read it back. Perhaps your intent
> is to write the value pointed to by yy to the file.
> That would be an indication that yo don't want to just
> write the direct contents of the test structure to the
> file.
>
> Another issue to consider is that of alignment. (char)
> may be byte aligned and (char *) may be doubleword
> aligned. The default alignment mode may insert dead space
> into items like a structure to improve access performance.
> When you intend to write items like structures to a file,
> stream, or other byte oriented device, you may want to note
> or remove this dead space. The #pragma align directive can
> be used to change this behavior.
>
> If your purpose will include reading this file back from
> another system, you may also want to note what order the
> bytes are written. In the case of multibyte objects, like
> a pointer or doubleword, different systems may presume the
> bytes are in a different order. This is important when
> the communication medium would be an IP based method.
>
> David