Portability across architectures

Shane Davis SHANE at UTDALVM1.BITNET
Thu Sep 8 07:03:53 AEST 1988


>I've run across a need to have data files in various forms of UN*X
>be portable to each other. Mostly, this deals with Intel to Motorola and
>vice-versa. I could write data out to files in ASCII, but this is cumbersome,
>slow and may hamper the products' marketability.
>
>The problem lies in writing integers as well as structures to files, and allow
> those files to be transferred between a multitude of machines without a data
> transformation taking place.
>
>A fellow programmer suggested an "XDR" standard from SUN, but this seems to onl
> work with inter-process communication. Has anyone encountered this problem??

XDR should do exactly what you need. Here is an example:

#include <stdio.h>
#include <rpc/rpc.h>
#define  MAXARRAYLEN 20

main()
  {
    XDR *xdrs;
    static unsigned int foo[MAXARRAYLEN],*fooptr,arraylen=MAXARRAYLEN,i=0;
    FILE *foo_out;

    foo_out = fopen ("fooarray","w");
    fooptr = &foo;
    while (i < 20)
      foo[i++] = i;
    xdrstdio_create (xdrs, foo_out, XDR_ENCODE);
    xdr_array (xdrs,&fooptr,&arraylen,MAXARRAYLEN,sizeof (int),xdr_int);
    fclose (foo_out);
  }

This program writes, in standard XDR binary representation, the entire contents
of the array 'foo', which can in turn be read by a program on another
architecture using XDR_DECODE rather than XDR_ENCODE. The last parameter to
the 'xdr_array' call is the name of the XDR "primitive" to be used on each
element of the array; as 'foo' is an int array, the function is 'xdr_int'.
Other primitives include 'xdr_float','xdr_short', etc. XDR functions are
also provided for structs.

Actually, I have not tested that program, but don't flame me too bad if it
doesn't work...

You can't move data from one architecture to another without *some* sort of
data transformation; XDR is much more compact and reasonable than ASCII files,
though.

--Shane Davis
  Systems Programmer, Univ. of Texas at Dallas Academic Computer Center
  SHANE@{UTDALVM1.BITNET|utdalvm1.dal.utexas.edu},rsd at engc1.dal.utexas.edu



More information about the Comp.lang.c mailing list