声明(cplusplus)
int snprintf ( char * s, size_t n, const char * format, ... );
/*
Parameters:
s
Pointer to a buffer where the resulting C-string is stored.
The buffer should have a size of at least n characters.
n
Maximum number of bytes to be used in the buffer.
The generated string has a length of at most n-1, leaving space for the additional terminating null character.
size_t is an unsigned integral type.
format
C string that contains a format string that follows the same specifications as format in printf (see printf for details).
... (additional arguments)
Depending on the format string, the function may expect a sequence of additional arguments, each containing a value to be used to replace a format specifier in the format string (or a pointer to a storage location, for n).
There should be at least as many of these arguments as the number of values specified in the format specifiers. Additional arguments are ignored by the function.
*/
Write formatted output to sized buffer.
Composes a string with the same text that would be printed if format
was used on printf
, but instead of being printed, the content is stored as a C string in the buffer pointed by s
(taking n
as the maximum buffer capacity to fill).
If the resulting string would be longer than n-1
characters, the remaining characters are discarded and not stored, but counted for the value returned by the function.
A terminating null character is automatically appended after the content written.
After the format
parameter, the function expects at least as many additional arguments
as needed for format
.
将格式化输出写入大小缓冲区。
用与 printf
使用 format
时打印的文本相同的字符串组成字符串,但不是打印,而是将内容作为C字符串存储在由 s
指向的缓冲区中(以n
为最大缓冲区填充容量)。
如果生成的字符串长度超过 n-1
个字符,则丢弃其余字符,不存储,而是将其计数为函数返回的值。
在写入内容后,将自动附加一个终止的空字符。
在format
参数之后,函数需要至少与 format
所需的 additional arguments
一样多。
Return Value
The number of characters that would have been written if n
had been sufficiently large, not counting the terminating null character.
If an encoding error occurs, a negative number is returned.
Notice that only when this returned value is non-negative and less than n
, the string has been completely written.
如果n
足够大,则会写入的字符数,不包括终止的空字符。
如果发生编码错误,则返回负数。
请注意,只有当此返回值为非负值且小于n
时,字符串才被完全写入。
Example
/* snprintf example */
#include <stdio.h>
int main ()
{
char buffer [100];
int cx;
cx = snprintf ( buffer, 100, "The half of %d is %d", 60, 60/2 );
if (cx>=0 && cx<100) // check returned value
snprintf ( buffer+cx, 100-cx, ", and the half of that is %d.", 60/2/2 );
puts (buffer);
return 0;
}
Output:
The half of 60 is 30, and the half of that is 15.
声明(msdn)
int snprintf(
char *buffer,
size_t count,
const char *format [,
argument] ...
);
int _snprintf(
char *buffer,
size_t count,
const char *format [,
argument] ...
);
/*
Parameters:
buffer
Storage location for the output.
count
Maximum number of characters to store.
format
Format-control string.
argument
Optional arguments.
*/
Return Value
Let len
be the length of the formatted data string, not including the terminating null. Both len
and count
are the number of characters for snprintf
and _snprintf
, and the number of wide characters for _snwprintf
.
For all functions, if len < count
, len
characters are stored in buffer
, a null-terminator is appended, and len
is returned.
The snprintf
function truncates the output when len
is greater than or equal to count
, by placing a null-terminator at buffer[count-1]
. The value returned is len
, the number of characters that would have been output if count
was large enough. The snprintf
function returns a negative value if an encoding error occurs.
For all functions other than snprintf
, if len = count
, len
characters are stored in buffer
, no null-terminator is appended, and len
is returned. If len > count
, count
characters are stored in buffer
, no null-terminator is appended, and a negative value is returned.
If buffer
is a null pointer and count
is zero, len
is returned as the count of characters required to format the output, not including the terminating null. To make a successful call with the same argument and locale parameters, allocate a buffer holding at least len + 1 characters.
If buffer
is a null pointer and count
is nonzero, or if format
is a null pointer, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, these functions return -1 and set errno to EINVAL.
设len
为格式化数据字符串的长度,不包括终止null。len
和count
都是snprintf
和_snprintf
的字符数,以及_snwprintf
的宽字符数。
对于所有函数,如果len < count
,则len
个字符存储在buffer
中,并附加空终止符,然后返回len
。
当len >= count
时,snprintf
函数通过在 buffer[count-1]
处放置空终止符来截断输出。
如果count
足够大,返回的值是len
,则会输出的字符数。如果发生编码错误,snprintf
函数将返回负值。
对于除snprintf
以外的所有函数,如果len=count
,则len
个字符存储在buffer
中,不追加空终止符,并返回len
。
如果len>count
,则count
个字符存储在buffer
中,不追加空终止符,并返回负值。
如果buffer
是空指针且count
为零,则返回len
作为格式化输出所需的字符计数,不包括终止的null。要使用相同的参数和区域设置参数成功调用,请分配一个至少包含len+1
个字符的缓冲区。
如果buffer
是空指针且count
非零,或者如果format
是空指针,则调用无效的参数处理程序,如参数验证中所述。如果允许继续执行,这些函数将返回-1并将errno设置为EINVAL。
Remarks
The snprintf
function and the _snprintf
family of functions format and store count or fewer characters in buffer. The snprintf
function always stores a terminating null character, truncating the output if necessary. The _snprintf
family of functions only appends a terminating null character if the formatted string length is strictly less than count characters. Each argument (if any) is converted and is output according to the corresponding format specification in format. The format consists of ordinary characters and has the same form and function as the format argument for printf. If copying occurs between strings that overlap, the behavior is undefined.
snprintf
函数和 _snprintf
函数系列在缓冲区中格式化并存储 count
个数或更少的字符。snprintf
函数始终存储一个终止的空字符NULL,必要时截断输出。如果格式化的字符串长度严格小于 count
个字符,则 _snprintf
系列函数只会附加一个终止的空字符。每个参数(如果有)都会转换,并根据format
中相应的格式规范输出。格式由普通字符组成,与printf
的format
参数具有相同的形式和功能。如果复制发生在重叠的字符串之间,则行为未定义。