How to predict size of outcome of vsprintf?

Barry Shein bzs at bu-cs.BU.EDU
Wed Mar 22 05:26:00 AEST 1989


Ok, there are two thoughts here:

	1. How to find out the number of characters a printf
	operation will generate.

	2. How to limit a printf operation to a buffer size.

I suggested the first might be the result of printing to a null
pointer (buffer or file.)

The second could be solved completely transparently by adding a format
effector which indicated the maximum buffer size ("must come first!")
Such as using %n# as in:

	sprintf(buf,"%80#%s",string);		/* 80 char buf including \0 */

or

	sprintf(buf,"%*#%s",BUFSIZ,string);	/* BUFSIZ char buf */

Not sure what "%n.m#" or "%#" might indicate (perhaps the latter would
just return a count of characters, ie, a zero length buffer?)

	-Barry Shein, Software Tool & Die



More information about the Comp.unix.wizards mailing list