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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!gatech!asuvax!ennews!envmsa.eas.asu.edu!ptran
  3. From: ptran@envmsa.eas.asu.edu (Phi-Long Tran)
  4. Subject: Re: While problem
  5. Message-ID: <22JUL199216531439@envmsa.eas.asu.edu>
  6. News-Software: VAX/VMS VNEWS 1.4-b1  
  7. Sender: news@ennews.eas.asu.edu (USENET News System)
  8. Organization: Arizona State University, Tempe, AZ
  9. References: <92204.160015GNR100@psuvm.psu.edu>
  10. Date: Wed, 22 Jul 1992 23:53:00 GMT
  11. Lines: 65
  12.  
  13. In article <92204.160015GNR100@psuvm.psu.edu>, GNR100@psuvm.psu.edu writes...
  14.  
  15. >   I'm having trouble with a whil loop in function I am writing to
  16. >read a line from a fileby puttingeach character into an array, accesed by
  17. >pionters, until the '\n' character is reached of EOF is reached.
  18. >Trouble is, I keep getting an error message that the function needs an
  19. >Lvalue (address) inside the while loop.  Any ideas as to the problem?
  20. >The filename is a global variable, and that's why it isn't declared.
  21.  
  22. >char readline(char *string[])
  23. >{
  24. >  int i=0;
  25. >  while(((*string+i++=(char)getc(fname))!=EOF)
  26. >      &&((*string+i++=(char)getc(fname))!='\n'));
  27. >  return (*string+(i-1));
  28. >}
  29.  
  30.      I commend your courage for even attempting to write this kind of
  31. gobbledegook -- I don't think even B. Kernighan or D. Ritchie would even
  32. try this stuff, let alone P.J.  Plauger.
  33.  
  34.      Here are my observations:
  35.  
  36. 1) Function getc accepts a file pointer ("FILE *"), *NOT* a file name.  You
  37. can find this out in *ANY* book on C -- just look for a description of the
  38. routine.
  39.  
  40. 2) You are doing too many things at once to even possibly debug reliably.
  41. Do you really understand all those operators, their precedence, their
  42. side-effects, etc. ad inifitum. et. al.
  43.  
  44. 3) To read in a line of text, you do not need an array of pointers (that's
  45. what "char *string[]" is and means).  You just need an array of characters
  46. into which a file I/O routine will fill with the characters.
  47.  
  48. 4) Why are you performing arithmetic operations on the left side of an
  49. assignment statement?  Look in a C book on making assignments.
  50.  
  51.      If all you want to do is read in a string, then there are many
  52. standard library functions you can call:  fgets, gets, fscanf, scanf ...
  53. They are declared in the header file "stdio.h" that you include in a module
  54. when you use them.
  55.  
  56.      #define MAX_INPUT_CHARS 132 /* Or some relevant value. */
  57.  
  58.      ...
  59.  
  60.      char szData[MAX_INPUT_CHARS];
  61.      FILE *pfileInput;           /* Pointer to a file for input. */
  62.  
  63.      ...
  64.  
  65.      if (fgets( szData, sizeof( szData ), pfileInput) == NULL)
  66.      {
  67.         /* An error occurred. */
  68.      }
  69.  
  70.      Above all ... LOOK IT UP IN A C BOOK.  Only post when you have
  71. EXHAUSTED your resources for information.  Have you looked this problem up?
  72.  
  73. ---
  74. Phi-Long Tran
  75. ptran@asuvax.eas.asu.edu
  76. Arizona State University
  77.