Guessing buffer size needed for sprintf beforehand

Patrick Powell papowell at attila.uucp
Sun May 15 01:34:44 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.
>| This requires two calls to the c print fuctions, one to get the
>| size and the second to actually print.  Since the first call
>| goes to dev/null it should be faster since it doesn't really
>| write anything? (Is this true?  For all UNIXs?).  This won't
>| work for VMS though.
>+---------------

(Brandon talks about v?printf).
>++Brandon
>-- 
>	      Brandon S. Allbery, moderator of comp.sources.misc
>	{well!hoptoad,uunet!marque,cbosgd,sun!mandrill}!ncoast!allbery
>Delphi: ALLBERY						     MCI Mail: BALLBERY

I suggest using the handy dandy little function,
snprintf( int count, char *buffer, <varargs stuff about format, etc.> )

This was proposed as a part of the "Standard C Library".  It was reject
for various reasons that I am not privy to.
I have reached the stage where I have re-implemented a portable version of
this that I use wherever I must do SPRINTF.

The lack of range and bound checking versions of 'standard'
routines in the library routines has been an obstacle to producing
portable and bombproof code.

Patrick Powell
Prof. Patrick Powell, Dept. Computer Science, 136 Lind Hall, 207 Church St. SE,
University of Minnesota,  Minneapolis, MN 55455 (612)625-3543/625-4002



More information about the Comp.unix.questions mailing list