Re: setting MAX characters emitted in printf() functions?



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
.



Relevant Pages

  • Re: setting MAX characters emitted in printf() functions?
    ... > Where printf() here is generic and refers to sprintf, fprintf, etc. ... > 256 characters on some platforms a double is even larger than that, ... > string containing the formatted variable if the desired field ...
    (comp.lang.c)
  • setting MAX characters emitted in printf() functions?
    ... Where printf() here is generic and refers to sprintf, fprintf, etc. ... 256 characters on some platforms a double is even larger than that, ... string containing the formatted variable if the desired field ...
    (comp.lang.c)
  • Re: sprintf and printf in Perl
    ... While 'printf' print the result directly to screen. ... sprintf and printf in Perl ... Output2 from screen (use sprintf for string) ... Useless use of sprintf in void context at math9.pl ...
    (perl.beginners)
  • Re: what about this new way of opening file
    ... ERROR undeclared non-standard function. ... The following defines the return value of printf. ... zero is a valid number of characters to transmit. ...
    (comp.lang.c)
  • Re: French characters
    ... > characters I want, which turns my problem into an entirely different ... > recieving an invalid key code that barfs, ... printf() can be very helpful in lots of circumstances. ... find the bug. ...
    (comp.os.linux.x)