sprintf_s、_sprintf_s_l、swprintf_s、_swprintf_s_lsprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l

将设置格式的数据写入字符串。Write formatted data to a string. CRT 中的安全性功能中所述,这些版本的 sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_l 具有安全性增强功能。These are versions of sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l with security enhancements as described in Security Features in the CRT.

语法Syntax

int sprintf_s(
   char *buffer,
   size_t sizeOfBuffer,
   const char *format,
   ...
);
int _sprintf_s_l(
   char *buffer,
   size_t sizeOfBuffer,
   const char *format,
   locale_t locale,
   ...
);
int swprintf_s(
   wchar_t *buffer,
   size_t sizeOfBuffer,
   const wchar_t *format,
   ...
);
int _swprintf_s_l(
   wchar_t *buffer,
   size_t sizeOfBuffer,
   const wchar_t *format,
   locale_t locale,
   ...
);
template <size_t size>
int sprintf_s(
   char (&buffer)[size],
   const char *format,
   ...
); // C++ only
template <size_t size>
int swprintf_s(
   wchar_t (&buffer)[size],
   const wchar_t *format,
   ...
); // C++ only

参数Parameters

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

sizeOfBuffersizeOfBuffer
可存储的最多字符数。Maximum number of characters to store.

formatformat
格式控件字符串Format-control string

......
要设置格式的可选参数Optional arguments to be formatted

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

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

返回值Return Value

写入的字符数; 如果出现错误,则为-1。The number of characters written, or -1 if an error occurred. 如果bufferformat为 null 指针,则sprintf_sswprintf_s将返回-1,并将errno设置为EINVALIf buffer or format is a null pointer, sprintf_s and swprintf_s return -1 and set errno to EINVAL.

sprintf_s返回缓冲区中存储的字节数,不包括终止 null 字符。sprintf_s returns the number of bytes stored in buffer, not counting the terminating null character. swprintf_s返回存储在缓冲区中的宽字符数,不包括终止 null 宽字符。swprintf_s returns the number of wide characters stored in buffer, not counting the terminating null wide character.

备注Remarks

Sprintf_s函数将一系列字符和值存储到缓冲区中。The sprintf_s function formats and stores a series of characters and values in buffer. 每个自变量(如果有)根据格式规范的相应格式规范进行转换和输出。Each argument (if any) is converted and output according to the corresponding format specification in format. 该格式包括普通字符,其形式和函数与printfformat参数相同。The format consists of ordinary characters and has the same form and function as the format argument for printf. null 字符追加在写入的最后一个字符后。A null character is appended after the last character written. 如果在重叠的字符串之间发生复制,则此行为不确定。If copying occurs between strings that overlap, the behavior is undefined.

Sprintf_ssprintf之间的一个主要区别在于sprintf_s检查格式字符串中的有效格式设置字符,而sprintf仅检查格式字符串或缓冲区是否为NULL指针。One main difference between sprintf_s and sprintf is that sprintf_s checks the format string for valid formatting characters, whereas sprintf only checks if the format string or buffer are NULL pointers. 如果任一检查失败,将调用无效参数处理程序,如 Parameter Validation中所述。If either check fails, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,则该函数将返回-1,并将errno设置为EINVALIf execution is allowed to continue, the function returns -1 and sets errno to EINVAL.

Sprintf_ssprintf之间的另一个主要区别是sprintf_s采用长度参数,该参数指定输出缓冲区的大小(以字符为限)。The other main difference between sprintf_s and sprintf is that sprintf_s takes a length parameter specifying the size of the output buffer in characters. 如果缓冲区对于格式化文本(包括终止 null)来说太小,则将缓冲区设置为空字符串,方法是在缓冲区[0] 处放置 null 字符,并调用无效的参数处理程序。If the buffer is too small for the formatted text, including the terminating null, then the buffer is set to an empty string by placing a null character at buffer[0], and the invalid parameter handler is invoked. _snprintf不同, sprintf_s保证缓冲区将以 null 结尾,除非缓冲区大小为零。Unlike _snprintf, sprintf_s guarantees that the buffer will be null-terminated unless the buffer size is zero.

swprintf_ssprintf_s的宽字符版本;swprintf_s的指针参数是宽字符字符串。swprintf_s is a wide-character version of sprintf_s; the pointer arguments to swprintf_s are wide-character strings. Swprintf_s中的编码错误检测可能与sprintf_s中的错误不同。Detection of encoding errors in swprintf_s may differ from that in sprintf_s. 这些带有 _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++, use of these functions is simplified by template overloads; the overloads can infer buffer length automatically, which eliminates 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.

有一些版本的sprintf_s ,可提供更多的控制(如果缓冲区太小)。There are versions of sprintf_s that offer additional control over what happens if the buffer is too small. 有关更多信息,请参见 _snprintf_s, _snprintf_s_l, _snwprintf_s, _snwprintf_s_lFor more information, see _snprintf_s, _snprintf_s_l, _snwprintf_s, _snwprintf_s_l.

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

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_stprintf_s_stprintf_s sprintf_ssprintf_s sprintf_ssprintf_s swprintf_sswprintf_s
_stprintf_s_l_stprintf_s_l _sprintf_s_l_sprintf_s_l _sprintf_s_l_sprintf_s_l _swprintf_s_l_swprintf_s_l

要求Requirements

例程所返回的值Routine 必需的标头Required header
sprintf_s_sprintf_s_lsprintf_s, _sprintf_s_l C: <stdio.h>C: <stdio.h>

C++: <cstdio> 或 <stdio.h>C++: <cstdio> or <stdio.h>
swprintf_s_swprintf_s_lswprintf_s, _swprintf_s_l C: <stdio.h> 或 <wchar.h>C: <stdio.h> or <wchar.h>

C++: <cstdio>、<cwchar>、<stdio.h> 或 <wchar.h>C++: <cstdio>, <cwchar>, <stdio.h> or <wchar.h>

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

示例Example

// crt_sprintf_s.c
// This program uses sprintf_s to format various
// data and place them in the string named buffer.
//

#include <stdio.h>

int main( void )
{
   char  buffer[200], s[] = "computer", c = 'l';
   int   i = 35, j;
   float fp = 1.7320534f;

   // Format and print various data:
   j  = sprintf_s( buffer, 200,     "   String:    %s\n", s );
   j += sprintf_s( buffer + j, 200 - j, "   Character: %c\n", c );
   j += sprintf_s( buffer + j, 200 - j, "   Integer:   %d\n", i );
   j += sprintf_s( buffer + j, 200 - j, "   Real:      %f\n", fp );

   printf_s( "Output:\n%s\ncharacter count = %d\n", buffer, j );
}
Output:
   String:    computer
   Character: l
   Integer:   35
   Real:      1.732053

character count = 79

示例Example

// crt_swprintf_s.c
// wide character example
// also demonstrates swprintf_s returning error code
#include <stdio.h>

int main( void )
{
   wchar_t buf[100];
   int len = swprintf_s( buf, 100, L"%s", L"Hello world" );
   printf( "wrote %d characters\n", len );
   len = swprintf_s( buf, 100, L"%s", L"Hello\xffff world" );
   // swprintf_s fails because string contains WEOF (\xffff)
   printf( "wrote %d characters\n", len );
}
wrote 11 characters
wrote -1 characters

请参阅See also

流 I/OStream I/O
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
scanf、_scanf_l、wscanf、_wscanf_lscanf, _scanf_l, wscanf, _wscanf_l
sscanf、_sscanf_l、swscanf、_swscanf_lsscanf, _sscanf_l, swscanf, _swscanf_l
vprintf 函数vprintf Functions