home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!utcsri!bonnie.concordia.ca!IRO.UMontreal.CA!CC.UMontreal.CA!nadeau
- From: nadeau@JSP.UMontreal.CA (Nadeau Jean-Claude)
- Newsgroups: comp.lang.c
- Subject: Re: Dropping a n from a fprintf()
- Message-ID: <1992Sep15.152857.15265@cc.umontreal.ca>
- Date: 15 Sep 92 15:28:57 GMT
- References: <1992Sep11.052108.26159@ra.msstate.edu>
- Sender: news@cc.umontreal.ca (Administration de Cnews)
- Organization: Universite de Montreal
- Lines: 36
-
- In article <1992Sep11.052108.26159@ra.msstate.edu> cee1@ra.msstate.edu (Charles Evans) writes:
- >Ok here is the deal.
- >
- >Reading in from a textfile with one string per line (variable length, but
- >none _too_ long) Then I want to add some strings on either side of the
- >read in string and rewrite the new string to a new file line by line. But
- >there are some probs.. here is what I have:
- >
- > FILE *fp1, *fp2;
- > char *item; /* also tried stuff like char item[20];,
- > same results */
- >
- >....
- >
- > while ((fgets(fp1,20,item)) != 0)
- > {
- > fprintf(fp2,"I found a %s in my pocket\n", item);
- > }
- >...
- >
- >Ok here is the deal. fgets reads at most 20-1 characters or up to a
- >newline.. and if it reaches a newline it adds that to the string along with
- >the \0.. So if on the first line of reading in (from fp1) it had "chicken"
- >the string in fprintf would be:
- >
- ... etc ...
-
- If you're sure that the lines are shorter than 20-1 characters
- (including the \n) you can use this short statement:
-
- item[ strlen(item) - 1 ] = '\0';
- --
- Jean-Claude Nadeau, ing. <nadeau@JSP.UMontreal.CA>
- Services informatiques <nadeau@ERE.UMontreal.CA>
- Universite de Montreal <nadeau@UMTLVR.BITNET>
- Tel. (514) 343-6111,ext 5234 Fax (514) 343-2155
-