Portable use of fread/fwrite with structs

M

mazsx

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.
 
S

slebetman

mazsx said:
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
 
T

Toni Uusitalo

mazsx said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top