vprintf_s、_vprintf_s_l、vwprintf_s、_vwprintf_s_lvprintf_s, _vprintf_s_l, vwprintf_s, _vwprintf_s_l

使用指向参数列表的指针编写格式化输出。Writes formatted output by using a pointer to a list of arguments. 这些版本的 vprintf、_vprintf_l、vwprintf、_vwprintf_l 具有安全增强功能,如 CRT 中的安全功能所述。These versions of vprintf, _vprintf_l, vwprintf, _vwprintf_l have security enhancements, as described in Security Features in the CRT.

语法Syntax

int vprintf_s(
   const char *format,
   va_list argptr
);
int _vprintf_s_l(
   const char *format,
   locale_t locale,
   va_list argptr
);
int vwprintf_s(
   const wchar_t *format,
   va_list argptr
);
int _vwprintf_s_l(
   const wchar_t *format,
   locale_t locale,
   va_list argptr
);

参数Parameters

formatformat
格式规范。Format specification.

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

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

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

返回值Return Value

vprintf_svwprintf_s返回写入的字符数,不包括终止 null 字符,或在出现输出错误时返回一个负值。vprintf_s and vwprintf_s return the number of characters written, not including the terminating null character, or a negative value if an output error occurs. 如果format为空指针,或如果格式字符串包含无效的格式字符,则将调用无效的参数处理程序,如参数验证中所述。If format is a null pointer, 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

其中每个函数都采用一个指向参数列表的指针,然后将给定数据格式化并写入stdoutEach of these functions takes a pointer to an argument list, then formats and writes the given data to stdout.

这些函数的安全版本仅与vprintfvwprintf不同,因为安全版本检查格式字符串是否包含有效的格式化字符。The secure versions of these functions differ from vprintf and vwprintf only in that the secure versions check that the format string contains valid formatting characters.

vwprintf_svprintf_s的宽字符版本;如果在 ANSI 模式下打开流,则这两个函数的行为相同。vwprintf_s is the wide-character version of vprintf_s; the two functions behave identically if the stream is opened in ANSI mode. vprintf_s当前不支持输出到 UNICODE 流中。vprintf_s doesn't currently support output into a UNICODE stream.

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

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

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_vtprintf_s_vtprintf_s vprintf_svprintf_s vprintf_svprintf_s vwprintf_svwprintf_s
_vtprintf_s_l_vtprintf_s_l _vprintf_s_l_vprintf_s_l _vprintf_s_l_vprintf_s_l _vwprintf_s_l_vwprintf_s_l

要求Requirements

例程所返回的值Routine 必需的标头Required header 可选标头Optional headers
vprintf_s_vprintf_s_lvprintf_s, _vprintf_s_l <stdio.h> 和 <stdarg.h><stdio.h> and <stdarg.h> <varargs.h>*<varargs.h>*
vwprintf_s_vwprintf_s_lvwprintf_s, _vwprintf_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.

通用 Windows 平台 (UWP) 应用中不支持控制台。The console is not supported in Universal Windows Platform (UWP) apps. 与控制台、 stdinstdoutstderr关联的标准流句柄必须重定向, 然后 C 运行时函数才能在 UWP 应用中使用它们。The standard stream handles that are associated with the console, stdin, stdout, and stderr, must be redirected before C run-time functions can use them in UWP apps. 有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.

请参阅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