home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.aix
- Path: sparky!uunet!spillman!tye
- From: tye@spillman.uucp (E. Tye McQueen)
- Subject: Re: Simple EOF problem?
- Message-ID: <1992Nov19.150644.28930@spillman.uucp>
- Date: Thu, 19 Nov 1992 15:06:44 GMT
- References: <BxvzEE.79B@acsu.buffalo.edu> <1992Nov18.074916.2886@aragorn.unibe.ch> <1270@ki.com>
- Organization: Spillman Data Systems
- Lines: 39
-
- dwatts@ki.com (Dan Watts) writes:
- )|In article <BxvzEE.79B@>, 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?
- )As mentioned above, fgetc returns int and EOF is -1. On AIX, chars
- )are *not* signed. So, the -1 gets truncated to 0xFF to fit into
- )buffer[]. The 255 is then compared to -1 and sure enough, they're
- )not equal.
-
- And that code will break on a SparcStationIPC running SunOS 4.1.2
- since reading in a character 0xff will cause fgetc(fp) to return 255
- which will get shoved into buffer[i++] and become -1 so the code
- will detect EOF when there isn't one.
-
- You can't encode end-of-file (of characters) with a character value
- unless you do something like old MS-DOS where they have "files of
- characters-other-than-CTRL-Z". But that was never a good idea.
-
- The code will also happilly overfill buffer[] for even moderately
- large files. You might try fread() instead (and its faster).
-
- tye@spillman.com Tye McQueen, E.
- ----------------------------------------------------------
- Nothing is obvious unless you are overlooking something.
- ----------------------------------------------------------
-