read(fd, &y, sizeof(y)) portability

Geoff Kuenning geoff at callan.UUCP
Tue Mar 20 06:36:15 AEST 1984


>	You are better off writing:
>
>		read (fd, (char *) &y, sizeof (y))
>
>	It makes a difference on some machines.

Actually, you are still non-portable if there is a possibility that the data
will be read on a machine different from the one it was written on.  Any of
the following problems might crop up:

	Character sizes differ (yes, there are still 6-, 7-, and 9-bit bytes
		out there--GCOS, for example, uses 9 bits)
	Long sizes differ (less likely but conceivable)
	Byte orderings differ

I ran into the last one trying to read the Bell distribution tapes on a
68000.  "cpio" writes the tape header with the type of construct suggested
above, but writes the tape contents in character form.  If I byte-swap the
contents appropriately, the header gets screwed up because of 68000/vax
byte ordering differences.  "cpio" has a switch ('-c') to solve this problem
by never writing binary data, but Bell (in their infinite wisdom) did not
use this option when writing their distribution tapes.

			 _ _ _ _ _ _ _
(Isn't every computer a |d|i|g|i|t|a|l| computer?)
			 - - - - - - -

			Geoff Kuenning
			Callan Data Systems
			...!ihnp4!sdcrdcf!trwrb!wlbr!callan!geoff



More information about the Comp.unix.wizards mailing list