Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!mips!twg.com!david From: david@twg.com (David S. Herron) Newsgroups: comp.sys.amiga Subject: Re: C standard across machines (question). Message-ID: <8198@gollum.twg.com> Date: 2 Nov 90 21:47:00 GMT References: <43072@eerie.acsu.Buffalo.EDU> Reply-To: david@twg.com (David S. Herron) Organization: The Wollongong Group, Palo Alto, CA Lines: 40 In article <43072@eerie.acsu.Buffalo.EDU> v069qqqc@ubvmsd.cc.buffalo.edu writes: >I am writing a general purpose database in C. ... >... I would therefore prefer to make the program (as >well as the database format) as portable as possible. > >The problem is, (correct me if I'm wrong) Intel machines are "little >endian" whereas Motorolas are "big endian". I assume C adopts one of >these standards to maintain protability of files between machines. ... No, C does not adopt a standard -- whatever the byte order is on the local system is what it is. The networking (TCP/IP) people came across the same problem loooong ago and came up with a Network Standard Byte Order. Grep around in /usr/include on your sun for "ntohl" to see how they're implemented. I forget where this ordering is defined, probably in an RFC somewhere. Probably it's that one which reprints the part of Gulliver's Travels detailing his experiences with the Endians..? >As it stands now, I'm using fread() and fwrite() for my file i/o. >I KNOW this must be wrong, since it's simply a binary image transfer. >Is there any general way to read/write a block of data so that this >mess can be resolved, or do I have to write code to output the data >byte-by-byte? No.. you can use f{read,write}() but before you fwrite() and after you fread() you should go through the buffer & swap all the bytes around. Usually this is implemented by having a layer of code through which all accesses to the object is done. (database in this case) In this layer you translate between internal & external representation appropriately as data is going through this layer. -- <- David Herron, an MMDF & WIN/MHS guy, <- Formerly: David Herron -- NonResident E-Mail Hack <- <- Use the force Wes!