L
Lauri Alanko
I would like to format some output into a buffer that is exactly of
the correct size to hold the formatted output _without_ a terminating
NUL. However, the snprintf function always ensures that the resulting
string is null-terminated, even if this means truncating the output.
This behavior is inconsistent with how strncpy works, and it seems to
be the wrong default: if snprintf did not ensure null-termination, the
caller could easily provide it if required. But there seems to be no
efficient way in the other direction.
Currently, I have to allocate a temporary buffer that is one byte
longer, then snprintf to the temporary buffer, then memcpy everything
but the terminating '\0' to the final destination buffer. Is there a
better way?
Thanks,
Lauri
the correct size to hold the formatted output _without_ a terminating
NUL. However, the snprintf function always ensures that the resulting
string is null-terminated, even if this means truncating the output.
This behavior is inconsistent with how strncpy works, and it seems to
be the wrong default: if snprintf did not ensure null-termination, the
caller could easily provide it if required. But there seems to be no
efficient way in the other direction.
Currently, I have to allocate a temporary buffer that is one byte
longer, then snprintf to the temporary buffer, then memcpy everything
but the terminating '\0' to the final destination buffer. Is there a
better way?
Thanks,
Lauri