home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11470 < prev    next >
Encoding:
Internet Message Format  |  1992-07-22  |  2.1 KB

  1. Path: sparky!uunet!mcsun!fuug!demos!kiae!glas!demos!grebyn.com!richk
  2. From: richk@grebyn.com
  3. Newsgroups: comp.lang.c
  4. Date: 17 Jul 92 20:53 MDT
  5. Subject: Re: (Help) dynamic use of sprintf ?
  6. Sender: Notesfile to Usenet Gateway <notes@glas.apc.org>
  7. Message-ID: <1992Jul17.165343.13102@grebyn.co>
  8. References: <brself.711345285@hal>
  9. Nf-ID: #R:brself.711345285@hal:945470687:1992Jul17.165343.13102@grebyn.co:800124447:001:1713
  10. Nf-From: grebyn.com!richk    Jul 17 20:53:00 1992
  11. Lines: 44
  12.  
  13.  
  14. In article <brself.711345285@hal> brself@hal.gnu.ai.mit.edu (Ben Self) writes:
  15. >
  16. >Could anyone please help me with a C programming dilemma that has plagued me
  17. >for ages.
  18. >
  19. >Often when formatting strings or converting numerics to alphas, sprintf ()
  20. >appears as an extremely attractive possibility.  Unfortunately, it does not
  21. >fit well with dynamic allocation nor is it easily safe guarded from 
  22. >segmentation faults and bus errors.
  23. >
  24. >I have tried many methods in the past, but am not happy with any yet tried.
  25. >
  26. >Some of these include:
  27. >
  28. >     . using the return value of fprintf () to a file pointer attached
  29. >       to /dev/null with the same arg format that I pass to sprintf ()
  30. >
  31. >     . determining the size of integers by log10 or (in the event I
  32. >       don't want to link to the math library) dividing by 10 until
  33. >       it becomes fractional.
  34. >
  35. >     . writing my own subset of sprintf () which takes care of this
  36. >       problem [far more work than it is worth].
  37. >
  38. >
  39. >We must sell -- any serious offer will be accepted :-)
  40. >
  41. >Thanks in advance.
  42.  
  43.     Yes... Anyone with a good answer please post!
  44.  
  45.     When I was struck with this problem I #define'd a symbol giving
  46. the number of characters I expected the sprintf to produce, plus a few
  47. extra, based on visual inspection of the format string.  I placed the
  48. #define close to it's use, and I put a big note on the definition
  49. saying how I picked the number, and imploring anyone who changed the
  50. format to re-count and change the #define.  In short, I just tried to
  51. make it obvious to any reader that this was a tricky area.
  52.  
  53. -- 
  54. Richard Krehbiel                                 richk@grebyn.com
  55. OS/2 2.0 will do for me until AmigaDOS for the 386 comes along...
  56.  
  57.