home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11637 < prev    next >
Encoding:
Text File  |  1992-07-27  |  1.9 KB  |  76 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!gatech!taco!SOMERVILLE@BAEPV3.NCSU.EDU
  3. From: somerville@BAEPV3.NCSU.EDU (SOMERVILLE)
  4. Subject: Re: While problem
  5. Message-ID: <0095E316.55D07C00@BAEPV3.NCSU.EDU>
  6. Sender: news@ncsu.edu (USENET News System)
  7. Reply-To: somerville@BAEPV3.NCSU.EDU (SOMERVILLE)
  8. Organization: North Carolina State University
  9. References: <92204.160015GNR100@psuvm.psu.edu>,<131@nixeidsc.sni.ie>
  10. Date: Mon, 27 Jul 1992 13:20:59 GMT
  11. Lines: 63
  12.  
  13. In article <131@nixeidsc.sni.ie>, bsullivn@nixeidsc.sni.ie (Bryan O'Sullivan) writes:
  14. >
  15. >
  16. >GNR100@psuvm.psu.edu writes:
  17.  
  18.  [Stuff Deleted]
  19.  
  20. >: char readline(char *string[])
  21. >: {
  22. >:   int i=0;
  23. >: 
  24. >:   while(((*string+i++=(char)getc(fname))!=EOF)
  25. >:       &&((*string+i++=(char)getc(fname))!='\n'));
  26. >:   return (*string+(i-1));
  27. >: }
  28. >
  29. >Well, first off, your call to getc will bomb if you ever get the rest of
  30. >the program running.  Getc(3) takes a FILE pointer, not a file name, as
  31. >argument.  Also, (no flame intended) you're making the classic new C
  32. >programmer mistake of trying to do too many "neat" things inside your
  33. >while loop, which is part of the reason why it isn't working.
  34. >
  35. >
  36. >char readline(char string[])
  37. >{
  38. >    int i;
  39. >
  40. >    for (i = 0; (string[i] = getc(fp)) != EOF; i++) {
  41. >        if (string[i] == '\n') {
  42. >            break;
  43. >        }
  44. >    }
  45. >    return &string[i-1];
  46. >}
  47. >
  48. >
  49. >[Rest Deleted]
  50.  
  51. Although this may work, I do not believe it is portable.
  52. If string is a char *, then
  53.  
  54.     (string[i] = getc( fp )) != EOF  
  55.  
  56. could be a problem.  getc() returns an int and EOF is a 
  57. negative value.  You do not know if the implementation treats chars 
  58. as signed or unsigned.  If you want to test for EOF, use an int to
  59. hold the return value from getc():
  60.  
  61.   int c;
  62.   .
  63.   .
  64.   ... (c = getc( fp )) != EOF ...
  65.   .
  66.   .
  67.      string[i] = c;
  68.  
  69.  
  70. I could certainly be wrong -- perhaps someone with a copy of the
  71. standard could comment on this. 
  72.  
  73. --
  74. Garth Somerville
  75. somerville@bae.ncsu.edu
  76.