Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!uflorida!haven!umd5!dzoey From: dzoey@umd5.umd.edu (Joe Herman) Newsgroups: comp.lang.c Subject: Re: Why unsigned chars not default? Message-ID: <4174@umd5.umd.edu> Date: 25 Oct 88 00:21:30 GMT References: <7354@ihlpl.ATT.COM> Distribution: na Organization: University of Maryland, College Park Lines: 55 From article <7354@ihlpl.ATT.COM>, by knudsen@ihlpl.ATT.COM (Knudsen): > In article <9563@pur-ee.UUCP>, mendozag@pur-ee.UUCP (Grado) writes: >> A guy around here is trying to port to several machines a program he >> hacked away in a PC using Lattice C. For some obscure reason in his >> original program he decided to use only low-level I/O. That forced >> him to "split" integers and then save them as 2 bytes and then later >> when the file is read back the integers are put together(!). Try this: union intaschar { char hilo[sizeof (int)]; int val; } foo; foo.val = somenumber write (fh, foo.hilo, sizeof (int)); if for some reason he can't just write the integer out like below. > > At least on a Motorola micro (6809 or 680x0) you can say > write(chan, int, 2) and put out the whole integer at once. Ick, I assume you mean: write (chan, &int, sizeof (int)); /* excuse the overloading of 'int' */ otherwise you're writing out two bytes of the address of 'int'. Also, for PC's (at least with microsoft) make sure you open the file in binary mode if you're going to do binary I/O. > I DO wish compilers would tell you somehow what the default is; > the 3B2 compilers seem to default to unsigned char, which breaks > a lot of old EOF loops. Remember, functions like getchar, getc &c, return an int, not a char which gets you around the problem of '\0377' being confused with EOF. > -- > Mike Knudsen Bell Labs(AT&T) att!ihlpl!knudsen > "Lawyers are like handguns and nuclear bombs. Nobody likes them, > but the other guy's got one, so I better get one too." Nice quote. Joe Herman The University of Maryland dzoey@terminus.umd.edu -- "Everything is wonderful until you know something about it."