vsprintf_s、_vsprintf_s_l、vswprintf_s、_vswprintf_s_lvsprintf_s, _vsprintf_s_l, vswprintf_s, _vswprintf_s_l

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

语法Syntax

int vsprintf_s(
   char *buffer,
   size_t numberOfElements,
   const char *format,
   va_list argptr
);
int _vsprintf_s_l(
   char *buffer,
   size_t numberOfElements,
   const char *format,
   locale_t locale,
   va_list argptr
);
int vswprintf_s(
   wchar_t *buffer,
   size_t numberOfElements,
   const wchar_t *format,
   va_list argptr
);
int _vswprintf_s_l(
   wchar_t *buffer,
   size_t numberOfElements,
   const wchar_t *format,
   locale_t locale,
   va_list argptr
);
template <size_t size>
int vsprintf_s(
   char (&buffer)[size],
   const char *format,
   va_list argptr
); // C++ only
template <size_t size>
int vswprintf_s(
   wchar_t (&buffer)[size],
   const wchar_t *format,
   va_list argptr
); // C++ only

参数Parameters

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

numberOfElementsnumberOfElements
缓冲区的大小(以字符为限)。Size of buffer in characters.

formatformat
格式规范。Format specification.

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

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

返回值Return Value

vsprintf_svswprintf_s返回写入的字符数,不包括终止 null 字符,或在出现输出错误时返回一个负值。vsprintf_s and vswprintf_s return the number of characters written, not including the terminating null character, or a negative value if an output error occurs. 如果缓冲区格式为 Null 指针,如果numberOfElements为零,或者格式字符串包含无效的格式字符,则将调用无效参数处理程序,如参数验证中所述。If buffer or format is a null pointer, if numberOfElements is zero, or if the format string contains invalid formatting characters, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则函数将返回-1,并将errno设置为EINVALIf execution is allowed to continue, the functions return -1 and set errno to EINVAL.

有关这些代码及其他错误代码的信息,请参阅 _doserrno、errno、_sys_errlist 和 _sys_nerrFor information on these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

备注Remarks

其中每个函数都采用一个指向参数列表的指针,然后将给定数据格式化并写入缓冲区指向的内存。Each of these functions takes a pointer to an argument list, and then formats and writes the given data to the memory pointed to by buffer.

vswprintf_s符合Vswprintf的 ISO C 标准,该标准需要类型为size_t的第二个参数countvswprintf_s conforms to the ISO C Standard for vswprintf, which requires the second parameter, count, of type size_t.

这些函数与不安全版本的差异仅在于安全版本支持位置参数。These functions differ from the non-secure versions only in that the secure versions support positional parameters. 有关详细信息,请参阅 printf_p 位置参数For more information, see printf_p Positional Parameters.

这些带有 _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.

在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 non-secure functions with their 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
_vstprintf_s_vstprintf_s vsprintf_svsprintf_s vsprintf_svsprintf_s vswprintf_svswprintf_s
_vstprintf_s_l_vstprintf_s_l _vsprintf_s_l_vsprintf_s_l _vsprintf_s_l_vsprintf_s_l _vswprintf_s_l_vswprintf_s_l

要求Requirements

例程所返回的值Routine 必需的标头Required header 可选标头Optional headers
vsprintf_s_vsprintf_s_lvsprintf_s, _vsprintf_s_l <stdio.h> 和 <stdarg.h><stdio.h> and <stdarg.h> <varargs.h>*<varargs.h>*
vswprintf_s_vswprintf_s_lvswprintf_s, _vswprintf_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_vsprintf_s.c
// Compile with: cl /W4 crt_vsprintf_s.c
// This program uses vsprintf_s to write to a buffer.
// The size of the buffer is determined by _vscprintf.

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>

void test( char const * const format, ... )
{
   va_list args;
   int len;
   char * buffer;

   va_start( args, format );
   len = _vscprintf( format, args ) // _vscprintf doesn't count
                               + 1; // terminating '\0'
   buffer = (char *) malloc( len * sizeof(char) );
   if ( NULL != buffer )
   {
      vsprintf_s( buffer, len, format, args );
      puts( buffer );
      free( buffer );
   }
   va_end( args );
}

int main( void )
{
   test( "%d %c %d", 123, '<', 456 );
   test( "%s", "This is a string" );
}
123 < 456
This is a string

请参阅See also

流 I/OStream I/O
vprintf 函数vprintf Functions
格式规范语法:printf 和 wprintf 函数Format Specification Syntax: printf and wprintf 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