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

  1. Path: sparky!uunet!utcsri!bonnie.concordia.ca!IRO.UMontreal.CA!CC.UMontreal.CA!nadeau
  2. From: nadeau@JSP.UMontreal.CA (Nadeau Jean-Claude)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Dropping a n from a fprintf()
  5. Message-ID: <1992Sep15.152857.15265@cc.umontreal.ca>
  6. Date: 15 Sep 92 15:28:57 GMT
  7. References: <1992Sep11.052108.26159@ra.msstate.edu>
  8. Sender: news@cc.umontreal.ca (Administration de Cnews)
  9. Organization: Universite de Montreal
  10. Lines: 36
  11.  
  12. In article <1992Sep11.052108.26159@ra.msstate.edu> cee1@ra.msstate.edu (Charles Evans) writes:
  13. >Ok here is the deal.
  14. >
  15. >Reading in from a textfile with one string per line (variable length, but
  16. >none _too_ long) Then I want to add some strings on either side of the
  17. >read in string and rewrite the new string to a new file line by line. But
  18. >there are some probs.. here is what I have:
  19. >
  20. >    FILE *fp1, *fp2; 
  21. >    char *item;         /* also tried stuff like char item[20];,
  22. >                same results */
  23. >
  24. >....
  25. >
  26. >    while ((fgets(fp1,20,item)) != 0)
  27. >    {
  28. >        fprintf(fp2,"I found a %s in my pocket\n", item);
  29. >    }
  30. >...
  31. >
  32. >Ok here is the deal. fgets reads at most 20-1 characters or up to a
  33. >newline.. and if it reaches a newline it adds that to the string along with
  34. >the \0.. So if on the first line of reading in (from fp1) it had "chicken"
  35. >the string in fprintf would be:
  36. >
  37.     ... etc ...
  38.  
  39.     If you're sure that the lines are shorter than 20-1 characters
  40.     (including the \n) you can use this short statement:
  41.  
  42.         item[ strlen(item) - 1 ] = '\0';
  43. -- 
  44. Jean-Claude Nadeau, ing.    <nadeau@JSP.UMontreal.CA>
  45. Services informatiques        <nadeau@ERE.UMontreal.CA>
  46. Universite de Montreal        <nadeau@UMTLVR.BITNET>
  47. Tel. (514) 343-6111,ext 5234    Fax  (514) 343-2155
  48.