_vcprintf_s、_vcprintf_s_l、_vcwprintf_s、_vcwprintf_s_l

使用指向参数列表的指针编写格式化输出到控制台。 _vcprintf、_vcprintf_l、_vcwprintf、_vcwprintf_l 的这些版本如 CRT 中的安全功能 所述,其安全得到了增强。

重要

此 API 不能用于在 Windows 运行时中执行的应用程序。有关详细信息,请参见 CRT functions not supported with /ZW(CRT 函数不支持使用/ZW)。

int _vcprintf(
   const char* format,
   va_list argptr
);
int _vcprintf(
   const char* format,
   locale_t locale,
   va_list argptr
);
int _vcwprintf_s(
   const wchar_t* format,
   va_list argptr
);
int _vcwprintf_s_l(
   const wchar_t* format,
   locale_t locale,
   va_list argptr
);

参数

  • format
    格式规范。

  • argptr
    指向参数列表的指针。

  • locale
    要使用的区域设置。

有关详细信息,请参阅格式规范语法:printf 和 wprintf 函数

返回值

读取字符数,或在发生错误时返回负值。

类似于这些安全版本较低的函数,如果 format 是 null 指针,无效参数调用处理程序,如 参数验证所述。 此外,与这些安全版本较低的函数不同,如果 format 不指定有效格式,无效参数将引发异常。 如果允许继续执行,这些函数返回错误代码并设置 errno 为该错误代码。 如果更具体的值不适用,默认值错误代码是 EINVAL。

备注

每个函数通过指针指向参数列表,然后布局和编写特定数据到控制台。 _vcwprintf_s 是 _vcprintf_s的宽字符版本。 它采用宽字符字符串作为参数。

这些带有 _l 后缀的函数版本相同,只不过它们使用通过的区域设置参数而不是当前线程区域设置。

安全说明安全说明

确保 format 不是用户定义的字符串。有关更多信息,请参见避免缓冲区溢出

一般文本例程映射

TCHAR.H 例程

未定义 _UNICODE & _MBCS

已定义 _MBCS

已定义 _UNICODE

_vtcprintf_s

_vcprintf_s

_vcprintf_s

_vcwprintf_s

_vtcprintf_s_l

_vcprintf_s_l

_vcprintf_s_l

_vcwprintf_s_l

要求

例程

必需的标头

可选标头

_vcprintf_s, _vcprintf_s_l

<conio.h> 和<stdarg.h>

<varargs.h>*

_vcwprintf_s, _vcwprintf_s_l

<stdio.h> 或 <wchar.h> 和 <stdarg.h>

<varargs.h>*

* 仅对 UNIX V 兼容性是必需的。

有关其他兼容性信息,请参见兼容性

示例

// crt_vcprintf_s.cpp
#include <conio.h>
#include <stdarg.h>

// An error formatting function used to print to the console.
int eprintf_s(const char* format, ...)
{
  va_list args;
  va_start(args, format);
  return _vcprintf_s(format, args);
}

int main()
{
   eprintf_s("  (%d:%d): Error %s%d : %s\n", 10, 23, "C", 2111,
           "<some error text>");
   eprintf_s("  (Related to symbol '%s' defined on line %d).\n",
           "<symbol>", 5 );
}
  

.NET Framework 等效项

System::Console::Write

请参见

参考

流 I/O

vprintf 函数

_cprintf、_cprintf_l、_cwprintf、_cwprintf_l

fprintf、_fprintf_l、fwprintf、_fwprintf_l

printf、_printf_l、wprintf、_wprintf_l

sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_l

va_arg、va_copy、va_end、va_start