home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / unix / aix / 11712 < prev    next >
Encoding:
Internet Message Format  |  1992-11-18  |  2.2 KB

  1. Path: sparky!uunet!ogicse!emory!sol.ctr.columbia.edu!zaphod.mps.ohio-state.edu!news.acns.nwu.edu!network.ucsd.edu!dkf
  2. From: dkf@chem.ucsd.edu (David Knight French)
  3. Newsgroups: comp.unix.aix
  4. Subject: Re: Simple EOF problem?
  5. Message-ID: <1eevguINN9hm@network.ucsd.edu>
  6. Date: 19 Nov 92 02:52:14 GMT
  7. Article-I.D.: network.1eevguINN9hm
  8. References: <BxvzEE.79B@acsu.buffalo.edu>
  9. Organization: University of California San Diego, Chemistry
  10. Lines: 61
  11. NNTP-Posting-Host: chedccf0.ucsd.edu
  12.  
  13. In article <BxvzEE.79B@acsu.buffalo.edu> vidya-v@acsu.buffalo.edu (vidyaranya) writes:
  14. >
  15. >The following piece of code FAILS to detect End of File
  16. >under AIX(i dont know the version currently!) running on
  17. >an IBM PowerServer 970.
  18. >------
  19. >    char    buffer[2048];
  20. >    int    i;
  21. >    FILE    *fp;
  22. >
  23. >    i=0;
  24. >        while( (buffer[i++]=fgetc(fp))!= EOF )
  25. >         ;
  26. >------
  27. >Exactly same piece of code runs on a SparcStationIPC running
  28. >SunOS 4.1.2 .
  29. >
  30. >What could be the problem?
  31. >
  32. >Thanks.
  33. >
  34. >Vidyaranya
  35. >vidya-v@acsu.buffalo.edu
  36.  
  37. The problem is that the RS/6000 has unsigned characters.  Since EOF is
  38. -1 this means the test in your while will never complete.  Here is how
  39. I would code the above while:
  40.  
  41.     while( (buffer[i++]=fgetc(fp), ! feof(fp))
  42.  
  43. I found this same problem in a program we tried to port to AIX v3.2 and
  44. I solved the problem as I show here.  At the time I didn't know that the
  45. RS/6000 had unsigned characters, but the following excerpt from "XPG4-Wide
  46. Characters" by Jeffreys Copeland & Haemer in the Nov92 edition of RS/Magazine
  47. explains it:
  48.  
  49.          Some hardware (like the RS/6000) has unsigned characters.  Since
  50.     EOF is -1, this means that the following loop is guaranteed to never
  51.     complete:
  52.         
  53.         char c;
  54.         while ( (c = getchar()) != EOF )
  55.             putchar( c );
  56.  
  57.     Of course, getchar() returns an int, and putchar() takes an int as its
  58.     argument, so the correct loop is:
  59.  
  60.         int ci;
  61.         while ( (ci = getchar()) != EOF )
  62.             putchar( ci );
  63.  
  64. I would assume the Sparc uses a signed char.
  65.  
  66. Hope this helps,
  67.             --Dave
  68.  
  69. -- 
  70. David Knight French        Email Addresses:        Phone:    
  71. UCSD - Dept of Chemistry    INTERNET: dkfrench@ucsd.edu      (voice/fax)
  72. 9500 Gilman Drive        UUCP:     ucsd!dkfrench        (619)534-4193 /
  73. San Diego, CA  92093-0314    BITNET:   dkfrench@ucsd.bitnet    (619)534-6255
  74.