Is there a function around somewhere that I can use to predict the space that sprintf( ) will need? IOW, can I call a function size_t predict_space( "%s\n", some_string ) that will return the length of the C-string that will result from sprintf( "%s\n", some_string )?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
- Equivalent of std::pair in C
Use can use snprintf() with a size of of 0 to find out exactly how many bytes will be required. The price is that the string is in effect formatted twice.
In C99
snprintf
(note: Windows and SUSv2, do not provide an implementation of snprintf (or _snprintf) conforming to the Standard):For example:
You can use
snprintf
for that, as inBut see Autoconf's portability notes on
snprintf
.In most cases, you can compute it by adding length of the string you are concatenating and taking max length for numeric values based on the format you used.