home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / c / 13494 < prev    next >
Encoding:
Text File  |  1992-09-10  |  1.9 KB  |  58 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!gatech!nntp.msstate.edu!cee1
  3. From: cee1@ra.msstate.edu (Charles Evans)
  4. Subject: Dropping a n from a fprintf()
  5. Message-ID: <1992Sep11.052108.26159@ra.msstate.edu>
  6. Organization: Mississippi State University
  7. Date: Fri, 11 Sep 1992 05:21:08 GMT
  8. Lines: 48
  9.  
  10. Ok here is the deal.
  11.  
  12. Reading in from a textfile with one string per line (variable length, but
  13. none _too_ long) Then I want to add some strings on either side of the
  14. read in string and rewrite the new string to a new file line by line. But
  15. there are some probs.. here is what I have:
  16.  
  17.     FILE *fp1, *fp2; 
  18.     char *item;         /* also tried stuff like char item[20];,
  19.                 same results */
  20.  
  21. ....
  22.  
  23.     while ((fgets(fp1,20,item)) != 0)
  24.     {
  25.         fprintf(fp2,"I found a %s in my pocket\n", item);
  26.     }
  27. ...
  28.  
  29. Ok here is the deal. fgets reads at most 20-1 characters or up to a
  30. newline.. and if it reaches a newline it adds that to the string along with
  31. the \0.. So if on the first line of reading in (from fp1) it had "chicken"
  32. the string in fprintf would be:
  33.  
  34.    ---------------------------------------- 
  35.    | c | h | i | c | k | e | n | \n | \0 |
  36.    ----------------------------------------
  37.      0   1   2   3   4   5   6   7    8 
  38.  
  39. but when I printout.. I get this input:
  40. ----
  41. I found a chicken
  42. in my pocket
  43. ----
  44.  
  45. I shoots the \n in the fprintf() .. I want it on one line
  46. so how may I ask can i do it.. I know scanf() has a %[^exclude_chars] con-
  47. version.. but printf() doesnt seem to. And if I use gets() .. cant read
  48. from a file
  49. fscanf(fp1,"%s",item)  .. seems to be giving EVERYTIME a Segmentation
  50. Fault (UNIX) in that line. so we went to fgets.. what can I do to get
  51. it all on one line.. thanks
  52.  
  53. -- 
  54. +--------------------+-----------------------+------------------------+
  55. |  Charles E. Evans  |  cee1@ra.msstate.edu  |  All things are green  |
  56. |    iDLE CHATTEr    |  cee1@MSSTATE.BITNET  |  unless they are not.  |
  57. +--------------------+-----------------------+------------------------+
  58.