home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / unix / aix / 11723 < prev    next >
Encoding:
Text File  |  1992-11-19  |  1.9 KB  |  50 lines

  1. Newsgroups: comp.unix.aix
  2. Path: sparky!uunet!spillman!tye
  3. From: tye@spillman.uucp (E. Tye McQueen)
  4. Subject: Re: Simple EOF problem?
  5. Message-ID: <1992Nov19.150644.28930@spillman.uucp>
  6. Date: Thu, 19 Nov 1992 15:06:44 GMT
  7. References: <BxvzEE.79B@acsu.buffalo.edu> <1992Nov18.074916.2886@aragorn.unibe.ch> <1270@ki.com>
  8. Organization: Spillman Data Systems
  9. Lines: 39
  10.  
  11. dwatts@ki.com (Dan Watts) writes:
  12. )|In article <BxvzEE.79B@>, vidya-v@acsu.buffalo.edu (vidyaranya) writes:
  13. )||> The following piece of code FAILS to detect End of File
  14. )||> under AIX(i dont know the version currently!) running on
  15. )||> an IBM PowerServer 970.
  16. )||> ------
  17. )||>    char    buffer[2048];
  18. )||>    int i;
  19. )||>    FILE    *fp;
  20. )||> 
  21. )||>    i=0;
  22. )||>         while( (buffer[i++]=fgetc(fp))!= EOF )
  23. )||>          ;
  24. )||> ------
  25. )||> Exactly same piece of code runs on a SparcStationIPC running
  26. )||> SunOS 4.1.2 .
  27. )||> 
  28. )||> What could be the problem?
  29. )As mentioned above, fgetc returns int and EOF is -1.  On AIX, chars
  30. )are *not* signed.  So, the -1 gets truncated to 0xFF to fit into
  31. )buffer[].  The 255 is then compared to -1 and sure enough, they're
  32. )not equal.
  33.  
  34. And that code will break on a SparcStationIPC running SunOS 4.1.2
  35. since reading in a character 0xff will cause fgetc(fp) to return 255
  36. which will get shoved into buffer[i++] and become -1 so the code
  37. will detect EOF when there isn't one.
  38.  
  39. You can't encode end-of-file (of characters) with a character value
  40. unless you do something like old MS-DOS where they have "files of
  41. characters-other-than-CTRL-Z".  But that was never a good idea.
  42.  
  43. The code will also happilly overfill buffer[] for even moderately
  44. large files.  You might try fread() instead (and its faster).
  45.  
  46.  tye@spillman.com                         Tye McQueen, E.
  47. ----------------------------------------------------------
  48.  Nothing is obvious unless you are overlooking something. 
  49. ----------------------------------------------------------
  50.