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