home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ogicse!emory!sol.ctr.columbia.edu!zaphod.mps.ohio-state.edu!news.acns.nwu.edu!network.ucsd.edu!dkf
- From: dkf@chem.ucsd.edu (David Knight French)
- Newsgroups: comp.unix.aix
- Subject: Re: Simple EOF problem?
- Message-ID: <1eevguINN9hm@network.ucsd.edu>
- Date: 19 Nov 92 02:52:14 GMT
- Article-I.D.: network.1eevguINN9hm
- References: <BxvzEE.79B@acsu.buffalo.edu>
- Organization: University of California San Diego, Chemistry
- Lines: 61
- NNTP-Posting-Host: chedccf0.ucsd.edu
-
- In article <BxvzEE.79B@acsu.buffalo.edu> vidya-v@acsu.buffalo.edu (vidyaranya) writes:
- >
- >The following piece of code FAILS to detect End of File
- >under AIX(i dont know the version currently!) running on
- >an IBM PowerServer 970.
- >------
- > char buffer[2048];
- > int i;
- > FILE *fp;
- >
- > i=0;
- > while( (buffer[i++]=fgetc(fp))!= EOF )
- > ;
- >------
- >Exactly same piece of code runs on a SparcStationIPC running
- >SunOS 4.1.2 .
- >
- >What could be the problem?
- >
- >Thanks.
- >
- >Vidyaranya
- >vidya-v@acsu.buffalo.edu
-
- The problem is that the RS/6000 has unsigned characters. Since EOF is
- -1 this means the test in your while will never complete. Here is how
- I would code the above while:
-
- while( (buffer[i++]=fgetc(fp), ! feof(fp))
-
- I found this same problem in a program we tried to port to AIX v3.2 and
- I solved the problem as I show here. At the time I didn't know that the
- RS/6000 had unsigned characters, but the following excerpt from "XPG4-Wide
- Characters" by Jeffreys Copeland & Haemer in the Nov92 edition of RS/Magazine
- explains it:
-
- Some hardware (like the RS/6000) has unsigned characters. Since
- EOF is -1, this means that the following loop is guaranteed to never
- complete:
-
- char c;
- while ( (c = getchar()) != EOF )
- putchar( c );
-
- Of course, getchar() returns an int, and putchar() takes an int as its
- argument, so the correct loop is:
-
- int ci;
- while ( (ci = getchar()) != EOF )
- putchar( ci );
-
- I would assume the Sparc uses a signed char.
-
- Hope this helps,
- --Dave
-
- --
- David Knight French Email Addresses: Phone:
- UCSD - Dept of Chemistry INTERNET: dkfrench@ucsd.edu (voice/fax)
- 9500 Gilman Drive UUCP: ucsd!dkfrench (619)534-4193 /
- San Diego, CA 92093-0314 BITNET: dkfrench@ucsd.bitnet (619)534-6255
-