Re: snprintf heads-up
| From: | Jon Ribbens | Date: | Fri, 08 Sep 2000 14:33:12 +0000 |
| Subject: | Re: snprintf heads-up | ||
| References: | 1 2 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-32676@lists.php.net to get a copy of this message | ||
Zeev Suraski <zeev@zend.com> wrote:
> > This behaviour is not particularly useful, because it means that if your
> > buffer turns out to be too small, and you want to extend it, you have no
> > way of knowing by how much you need to increase it.
>
> If you want to extend it, that's true. However, in my opinion, in most
> cases you use snprintf() to give a maximum limit to an existing buffer,
> and you wouldn't increase its length even if it overflows.
This may indeed be the most common case, but from the point of view of
the people writing C libraries, they need to cater for as many people
as possible. One behaviour satisfies only some people, the other satisfies
everyone.
> In this (general, IMHO) case, the most interesting piece of information
> would be the number of characters actually written, so that you don't have
> to go through strlen() again.
You don't need strlen(), simply do max(snprintf(...), buffer_size)
Cheers
Jon