Re: setting MAX characters emitted in printf() functions?
- From: dj3vande@xxxxxxxxxxxxxxxxxxx (Dave Vandervies)
- Date: Tue, 5 Jul 2005 22:36:18 +0000 (UTC)
In article <daevaj$9n3$1@xxxxxxxxxxxxxxxx>,
David Mathog <mathog@xxxxxxxxxxx> wrote:
>Is there some method for restricting the maximum number of
>characters that will be emitted by a printf() function?
>Where printf() here is generic and refers to sprintf, fprintf, etc.
[snip]
> Since that seems not to be implemented in C
>the closest I've come is to allocate a largish array and look
>at the oc value set by sprintf(), then write * instead of the
>string containing the formatted variable if the desired field
>width is exceeded.
If you can depend on having access to C99 or any of several pre-C99
implementations that provide it as an extension (including both GNUC and
MSVC++, which are the two I have readily available to check), you can
do something like this using snprintf instead of sprintf, which reduces
the size of the array you need to sprintf into.
C99 snprintf returns the number of characters that would have been
written if the requested limit was high enough, so you can use the same
check you'd use with sprintf; GNUC snprintf and MSVC++ _snprintf return
negative values if they truncate the output, so you'd want to check
for that instead of a return value greater than the size you ask for.
(The C99 behavior is more useful in general but both give you the
information you need here.)
I don't know of any way to limit the size of the output of any of the
printf family of functions other than snprintf.
dave
--
Dave Vandervies dj3vande@xxxxxxxxxxxxxxxxxxx
I have read all the pertinent "man" pages, sprinkled chicken blood on the
terminal, and uttered all the appropriate curses. So it is time to seek help
from a greater power. --Peter A. Buhr in uw.mfcf.gripe
.
- References:
- setting MAX characters emitted in printf() functions?
- From: David Mathog
- setting MAX characters emitted in printf() functions?
- Prev by Date: strto[u]l and ERANGE
- Next by Date: Re: will the memory allocated by malloc get released when program exits?
- Previous by thread: setting MAX characters emitted in printf() functions?
- Next by thread: Re: setting MAX characters emitted in printf() functions?
- Index(es):
Relevant Pages
|
|