Re: Writing a structure

From: Keith Thompson (kst-u_at_mib.org)
Date: 04/06/04


Date: Tue, 06 Apr 2004 18:48:02 GMT

Dan.Pop@cern.ch (Dan Pop) writes:
> In <eb99b94a.0404052318.582c66f8@posting.google.com>
> glenmendez@yahoo.co.in (Glen) writes:
>
> >Is it possible to write a structure to a file in c...as in c++...??
> >is it using fwrite??
>
> There is more than one way of doing it. The two most popular approaches
> are:
>
> 1. Use a text file, and convert each field of the structure to a textual
> representation, using fprintf.
>
> 2. Use a binary file and dump the binary representation of the
> structure value with fwrite.
>
> The first approach is more portable (the value can be read on a different
> platform, or even with a program written in a different language), but
> uses more disk space and CPU cycles.
>
> The second approach uses less disk space and CPU cycles, but the resulting
> binary file can be read only by a C program compiled with the same
> compiler, on the same platform.
[...]

You're not guaranteed that the binary file can be read by a C program
compiled with a different compiler or on a different platform, but
*sometimes* it can be, if you're very careful and/or very lucky.

For different compilers on the same platform, compiler writers
typically use the same layout algorithms to allow data portability.
(This is not guaranteed; it's something you need to verify.)

It's not an approach I'd recommend, but I've worked on a system that
shares binary data files between VAX and Alpha, and on another that
shared binary data files between 68k and SPARC. In the former case,
we had to be careful to use the same floating-point formats on both
systems; it's not clear that the system could have met its performance
constraints if we had used a textual format. In the latter, I had to
insert dummy character array members to force common alignment; it was
ugly, but it worked.

This kind of thing is unlikely to work if the platforms have different
byte ordering.

But if you want to share binary data, be aware that you're going to be
spending a lot of your time keeping everything consistent, and
probably dealing with problems when things aren't consistent.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"


Relevant Pages

  • Re: Writing a structure
    ... Use a binary file and dump the binary representation of the ... >> compiler, on the same platform. ... Dan Pop ...
    (comp.lang.c)
  • Re: Writing a structure
    ... Dan.Pop@cern.ch (Dan Pop) wrote: ... >>You're not guaranteed that the binary file can be read by a C program ... >>For different compilers on the same platform, ...
    (comp.lang.c)
  • Re: interesting use of NEXT SENTENCE vs. CONTINUE
    ... These days I don't do compiler maintenance, but I HAVE done in the past, so ... You had a chance to point out how adherence to the standard can improve ... >> compile it WITH MINOR MODIFICATIONS for a specific platform. ... > platform-specific extension to COBOL ...
    (comp.lang.cobol)
  • Re: interesting use of NEXT SENTENCE vs. CONTINUE
    ... I DID think any old compiler would be able to ... > compile it WITH MINOR MODIFICATIONS for a specific platform. ... > not going to go to the Standard to see what SHOULD work. ... If you write your COBOL code avoiding the things that are marked as ...
    (comp.lang.cobol)
  • Re: Standard Ada Preprocessor
    ... because Ada cannot deal with the portability issues. ... > you are interfacing directly to some special-purpose hardware. ... by their nature not compiler ... one platform you get 3 members back, and on another you get 5, ...
    (comp.lang.ada)