[comp.lang.c] Portability: Ultimate challenge

00R0DHESI%bsu.csnet@relay.cs.net (Rahul Dhesi) (01/09/87)

This is a real challenge.  How does one write a truly portable program,
independent of character size, word size, byte ordering, and structure packing,
that will do file I/O that includes binary values? 

Specifically, I want to read/write file data that includes longs, ints, and
chars, in such a way that it can be read back on ANY machine by recompiling the
same source code on that machine.  Machine-dependent headers are permissible
but different executable code for different machines defeats the whole idea of
writing portable code. 

I can recreate binary values by reading and writing bytes and assembling and
disassembling them, BUT I haven't solved the problem of what to do if the host
machine happens to have 7-bit chars only.  (And if I always disassemble
everything into 7-bit units, what if the host only has 6-bit chars?  etc.)
Also, I haven't figured out how to allow for one's complement machines, and
other machines which may represent values with different bit patterns. 

File storage as text only is not practical because it's too inefficient and
would not be independent of ASCII vs EBCDIC etc. anyway. 

                                Rahul Dhesi <dhesi%bsu@csnet-relay.ARPA>
                                !seismo!csnet-relay.ARPA!bsu!dhesi
                                (Return path on message may not work for 
                                individual replies.)