Re: Writing a structure
From: Keith Thompson (kst-u_at_mib.org)
Date: 04/06/04
- Next message: E. Robert Tisdale: "Troll Alert: Comeau C99 Compiler more reliable than the standards documents"
- Previous message: Christopher Benson-Manica: "Re: function sprintf"
- In reply to: Dan Pop: "Re: Writing a structure"
- Next in thread: Dan Pop: "Re: Writing a structure"
- Reply: Dan Pop: "Re: Writing a structure"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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"
- Next message: E. Robert Tisdale: "Troll Alert: Comeau C99 Compiler more reliable than the standards documents"
- Previous message: Christopher Benson-Manica: "Re: function sprintf"
- In reply to: Dan Pop: "Re: Writing a structure"
- Next in thread: Dan Pop: "Re: Writing a structure"
- Reply: Dan Pop: "Re: Writing a structure"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|