home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / c / 13639 < prev    next >
Encoding:
Internet Message Format  |  1992-09-14  |  2.9 KB

  1. Path: sparky!uunet!mcsun!news.funet.fi!hydra!klaava!wirzeniu
  2. From: wirzeniu@klaava.Helsinki.FI (Lars Wirzenius)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: dropping a n from a f
  5. Message-ID: <1992Sep14.150048.1246@klaava.Helsinki.FI>
  6. Date: 14 Sep 92 15:00:48 GMT
  7. References: <199214.4396.25154@dosgate>
  8. Distribution: comp
  9. Organization: University of Helsinki
  10. Lines: 58
  11.  
  12. "alan popow" <alan.popow@canrem.com> answers a query about reading lines
  13. from text lines without reading the line-terminating newline with a
  14. suggestion to use the following code:
  15.  
  16. >     FILE *fp1, *fp2;
  17. >     char *item = "";
  18. >     int i = 0;
  19. >
  20. >     while(((item[i] = fgetc(fp1)) != '\n') && (i < 20))
  21. >         i++;
  22. >
  23. >     item[i] = '\0';
  24.  
  25. While I realize that this most probably was only meant as a guideline (I
  26. try to point out when I post code like this, myself), but being in a
  27. particularly nitpicky mood, I want to point out some shortcomings of the
  28. code.  This is not intended as a flame, only as constructive criticism.
  29. (I consider any code posted on the Net to be a valid target for such
  30. criticism, and of course all of my articles, especially nitpicking ones
  31. like this.) 
  32.  
  33. The above code tries to accomplish the same thing as the following:
  34.  
  35.     fgets(item, 20, fp1)
  36.     ...and then get rid of the newline at the end...
  37.  
  38. However, it does not quite succeed, for two reasons:
  39.  
  40. 1.  The hopefully familiar problem of storing the result of
  41.     fgetc/getc/getchar into a char, not an int, which leads to problems
  42.     with distinguishing EOF from a valid value for a char.  The range of
  43.     the values that fgetc/getc/getchar return is greater than the range
  44.     of values which a char variable can take, so storing the result into
  45.     a char looses information. 
  46.  
  47. 2.  Not checking for EOF.  The program will store garbage values into
  48.     the string if it tries to read past the end of the file.  Please
  49.     remember to check for error codes whenever you can (there a some
  50.     valid places where it is often not necessary to check for error
  51.     codes, but they are rather rare and can be the result of bad
  52.     interface design, e.g.  the return value of printf).  As soon as one
  53.     starts believing that "it can't happen to me", it will. 
  54.  
  55. Also, since the initialization of the `item' variable looks suspect, I
  56. think I should warn about storage allocation here: there is no code
  57. shown that will allocate memory to hold the string itself, only the
  58. pointer.  Unless you explicitly allocate some storage and set `item' to
  59. point at that storage, you will overwrite things in memory that you
  60. shouldn't overwrite (namely, in this case, the string literal; in the
  61. more usual case, with no initialization, random places in memory will be
  62. overwritten). 
  63.  
  64. (The initialization seems suspect since it is unnecessary -- there is no
  65. need to make `item' point to a string literal and later make it point to
  66. some other place to which you can store the strings that you read.)
  67.  
  68. --
  69. Lars.Wirzenius@helsinki.fi
  70.