By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,007 Members | 1,023 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,007 IT Pros & Developers. It's quick & easy.

Portable use of fread/fwrite with structs

P: n/a
Is there a portable way to use binary files?
By portability I mean:
Machine A (a supercomputer) with some compiler, my job runs there and
outputs several megabytes of data. To save space and for better
seekability, I use a binary format.
Machine B (my desktop) with some other compiler analizes this data.
So far I assumed that both architectures have the standard
representation of the float, double and int types.
I would like to use now structs, where the programs compiled for the
two machines may use different
representation, even if they both obey to the same (implementation
defined) arithmetic standard.
Can one still use structs in fread/fwrite, or only a member-by-member
input/output is possible.

Nov 15 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
mazsx wrote:
Is there a portable way to use binary files?
By portability I mean:
Machine A (a supercomputer) with some compiler, my job runs there and
outputs several megabytes of data. To save space and for better
seekability, I use a binary format.
Machine B (my desktop) with some other compiler analizes this data.
So far I assumed that both architectures have the standard
representation of the float, double and int types.
I would like to use now structs, where the programs compiled for the
two machines may use different
representation, even if they both obey to the same (implementation
defined) arithmetic standard.
Can one still use structs in fread/fwrite, or only a member-by-member
input/output is possible.


I'd suggest storing things in network byte order simply because you can
then use htons/ntohs & friends without writing your own endian
conversion libraries.

As for storing structs, I'd suggest properly serialising them by
writing & reading member-by-member. This means you need a
struct-to-bytearray & bytearray-to-struct function for each struct you
intend to store.

Also, I strongly recommend that you document the file representation of
the struct so
1. you have a reference for debugging your input/output.
2. code written in other languages in the future can read the file

Nov 15 '05 #2

P: n/a
mazsx wrote:
Is there a portable way to use binary files?
By portability I mean:
Machine A (a supercomputer) with some compiler, my job runs there and
outputs several megabytes of data. To save space and for better
seekability, I use a binary format.
Machine B (my desktop) with some other compiler analizes this data.
So far I assumed that both architectures have the standard
representation of the float, double and int types.
I would like to use now structs, where the programs compiled for the
two machines may use different
representation, even if they both obey to the same (implementation
defined) arithmetic standard.
Can one still use structs in fread/fwrite, or only a member-by-member
input/output is possible.


This is a faq (see question 20.5)
http://www.eskimo.com/~scs/C-faq/top.html
for xdr implementation see for example http://www.ossp.org/ (library
called OSSP xds).

with respect,
Toni Uusitalo
Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.