vsnprintf_s、_vsnprintf_s、_vsnprintf_s_l、_vsnwprintf_s、_vsnwprintf_s_lvsnprintf_s, _vsnprintf_s, _vsnprintf_s_l, _vsnwprintf_s, _vsnwprintf_s_l

使用指向参数列表的指针写入格式化的输出。Write formatted output using a pointer to a list of arguments. 这些版本的 vsnprintf、_vsnprintf、_vsnprintf_l、_vsnwprintf、_vsnwprintf_l 具有安全增强功能,如 CRT 中的安全功能所述。These are versions of vsnprintf, _vsnprintf, _vsnprintf_l, _vsnwprintf, _vsnwprintf_l with security enhancements as described in Security Features in the CRT.

语法Syntax

int vsnprintf_s(
   char *buffer,
   size_t sizeOfBuffer,
   size_t count,
   const char *format,
   va_list argptr
);
int _vsnprintf_s(
   char *buffer,
   size_t sizeOfBuffer,
   size_t count,
   const char *format,
   va_list argptr
);
int _vsnprintf_s_l(
   char *buffer,
   size_t sizeOfBuffer,
   size_t count,
   const char *format,
   locale_t locale,
   va_list argptr
);
int _vsnwprintf_s(
   wchar_t *buffer,
   size_t sizeOfBuffer,
   size_t count,
   const wchar_t *format,
   va_list argptr
);
int _vsnwprintf_s_l(
   wchar_t *buffer,
   size_t sizeOfBuffer,
   size_t count,
   const wchar_t *format,
   locale_t locale,
   va_list argptr
);
template <size_t size>
int _vsnprintf_s(
   char (&buffer)[size],
   size_t count,
   const char *format,
   va_list argptr
); // C++ only
template <size_t size>
int _vsnwprintf_s(
   wchar_t (&buffer)[size],
   size_t count,
   const wchar_t *format,
   va_list argptr
); // C++ only

参数Parameters

bufferbuffer
输出的存储位置Storage location for output.

sizeOfBuffersizeOfBuffer
用于输出的缓冲区大小,作为字符计数。The size of the buffer for output, as the character count.

countcount
要写入的字符最大数量(不包括终止 null 或 _TRUNCATE)。Maximum number of characters to write (not including the terminating null), or _TRUNCATE.

formatformat
格式规范。Format specification.

argptrargptr
指向参数列表的指针。Pointer to list of arguments.

localelocale
要使用的区域设置。The locale to use.

有关更多信息,请参见 格式规范For more information, see Format Specifications.

返回值Return Value

vsnprintf_s_vsnprintf_s_vsnwprintf_s返回写入的字符数,不包括终止 null,或者在出现输出错误时返回一个负值。vsnprintf_s, _vsnprintf_s and _vsnwprintf_s return the number of characters written, not including the terminating null, or a negative value if an output error occurs. vsnprintf_s_vsnprintf_s相同。vsnprintf_s is identical to _vsnprintf_s. 提供vsnprintf_s是为了符合 ANSI 标准。vsnprintf_s is included for compliance to the ANSI standard. 保留 _vnsprintf是为了向后兼容。_vnsprintf is retained for backward compatibility.

如果存储数据和终止 null 值所需的存储空间超过sizeOfBuffer,则会调用无效的参数处理程序,如参数验证中所述,除非count_TRUNCATE,在这种情况下,最多写入缓冲区中的字符串,并返回-1。If the storage required to store the data and a terminating null exceeds sizeOfBuffer, the invalid parameter handler is invoked, as described in Parameter Validation, unless count is _TRUNCATE, in which case as much of the string as will fit in buffer is written and -1 returned. 如果执行在无效的参数处理程序之后继续,则这些函数将缓冲区设置为空字符串,将Errno设置为ERANGE,并返回-1。If execution continues after the invalid parameter handler, these functions set buffer to an empty string, set errno to ERANGE, and return -1.

如果bufferformat指针,或者count小于或等于零,则调用无效的参数处理程序。If buffer or format is a NULL pointer, or if count is less than or equal to zero, the invalid parameter handler is invoked. 如果允许执行继续, 则这些函数会将errno设置为EINVAL , 并返回-1。If execution is allowed to continue, these functions set errno to EINVAL and return -1.

错误条件Error Conditions

状态Condition 返回Return errnoerrno
缓冲区NULLbuffer is NULL -1-1 EINVALEINVAL
格式NULLformat is NULL -1-1 EINVALEINVAL
计数< = 0count <= 0 -1-1 EINVALEINVAL
sizeOfBuffer太小(和Count ! = _TRUNCATEsizeOfBuffer too small (and count != _TRUNCATE) -1 (并将缓冲区设置为空字符串)-1 (and buffer set to an empty string) ERANGEERANGE

备注Remarks

其中每个函数都采用一个指向参数列表的指针,然后将给定数据的最多个字符的计数字符写入缓冲区,并追加一个终止 null。Each of these functions takes a pointer to an argument list, then formats and writes up to count characters of the given data to the memory pointed to by buffer and appends a terminating null.

如果count_TRUNCATE,则这些函数会将尽可能多的字符串写入缓冲区,同时为终止 null 留出空间。If count is _TRUNCATE, then these functions write as much of the string as will fit in buffer while leaving room for a terminating null. 如果整个字符串(包括终止 null)都适合缓冲区,则这些函数将返回写入的字符数(不包括终止 null);否则,这些函数将返回-1 以指示发生了截断。If the entire string (with terminating null) fits in buffer, then these functions return the number of characters written (not including the terminating null); otherwise, these functions return -1 to indicate that truncation occurred.

这些带有 _l后缀的函数的版本相同,只不过它们使用传入的区域设置参数而不是当前线程区域设置。The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current thread locale.

重要

确保 format不是用户定义的字符串。Ensure that format is not a user-defined string. 有关详细信息,请参阅 避免缓冲区溢出For more information, see Avoiding Buffer Overruns.

备注

若要确保终止 null 的空间,请确保计数严格小于缓冲区长度,或使用 _TRUNCATETo ensure that there is room for the terminating null, be sure that count is strictly less than the buffer length, or use _TRUNCATE.

在 C++ 中,使用这些函数由模板重载简化;重载可以自动推导出缓冲区长度 (不再需要指定大小自变量),并且它们可以自动用以更新、更安全的对应物替换旧的、不安全的函数。In C++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically (eliminating the need to specify a size argument) and they can automatically replace older, non-secure functions with their newer, secure counterparts. 有关详细信息,请参阅 Secure Template OverloadsFor more information, see Secure Template Overloads.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_vsntprintf_s_vsntprintf_s _vsnprintf_s_vsnprintf_s _vsnprintf_s_vsnprintf_s _vsnwprintf_s_vsnwprintf_s
_vsntprintf_s_l_vsntprintf_s_l _vsnprintf_s_l_vsnprintf_s_l _vsnprintf_s_l_vsnprintf_s_l _vsnwprintf_s_l_vsnwprintf_s_l

要求Requirements

例程所返回的值Routine 必需的标头Required header 可选标头Optional headers
vsnprintf_svsnprintf_s <stdio.h> 和 <stdarg.h><stdio.h> and <stdarg.h> <varargs.h>*<varargs.h>*
_vsnprintf_s_vsnprintf_s_l_vsnprintf_s, _vsnprintf_s_l <stdio.h> 和 <stdarg.h><stdio.h> and <stdarg.h> <varargs.h>*<varargs.h>*
_vsnwprintf_s_vsnwprintf_s_l_vsnwprintf_s, _vsnwprintf_s_l <stdio.h> 或 <wchar.h> 和 <stdarg.h><stdio.h> or <wchar.h>, and <stdarg.h> <varargs.h>*<varargs.h>*

* 仅对 UNIX V 兼容性是必需的。* Required for UNIX V compatibility.

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.

示例Example

// crt_vsnprintf_s.cpp
#include <stdio.h>
#include <wtypes.h>

void FormatOutput(LPCSTR formatstring, ...)
{
   int nSize = 0;
   char buff[10];
   memset(buff, 0, sizeof(buff));
   va_list args;
   va_start(args, formatstring);
   nSize = vsnprintf_s( buff, _countof(buff), _TRUNCATE, formatstring, args);
   printf("nSize: %d, buff: %s\n", nSize, buff);
   va_end(args);
}

int main() {
   FormatOutput("%s %s", "Hi", "there");
   FormatOutput("%s %s", "Hi", "there!");
   FormatOutput("%s %s", "Hi", "there!!");
}
nSize: 8, buff: Hi there
nSize: 9, buff: Hi there!
nSize: -1, buff: Hi there!

请参阅See also

流 I/OStream I/O
vprintf 函数vprintf Functions
fprintf、_fprintf_l、fwprintf、_fwprintf_lfprintf, _fprintf_l, fwprintf, _fwprintf_l
printf、_printf_l、wprintf、_wprintf_lprintf, _printf_l, wprintf, _wprintf_l
sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_lsprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l
va_arg、va_copy、va_end、va_startva_arg, va_copy, va_end, va_start