Guessing buffer size needed for sprintf beforehand

David Hitz hitz at mips.COM
Mon May 16 15:53:55 AEST 1988


In article <7768 at ncoast.UUCP> allbery at ncoast.UUCP (Brandon Allbery) writes:
>As quoted from <136 at insyte.uucp> by jad at insyte.uucp (Jill Diewald):
>+---------------
>| One solution is to fprintf into a file opened to be /dev/null.
[...]
>Close.  fprintf() to a newly-created temporary file, then rewind() it (this
>is a stdio function) and getc() out of it and send it wherever.  If you need
>the size, get the file size using the OS's current-file-position call before
>the rewind().  This is perhaps a bit slower than an in-core buffer, but it's
>clean and is limited only by free space on the disk!

Oops: SYSV vs. BSD.

In SYSV printf() returns the number of characters printed, so Jill's
solution works fine without Brandon's gyrations.

In BSD printf() returns the first argument.
-- 
Dave Hitz					home: 408-739-7116
UUCP: {decvax,ucbvax,ihnp4}!decwrl!mips!hitz 	play: 408-991-0345



More information about the Comp.unix.questions mailing list