写特定格式字符串到另一个字符串sprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l

 

Write formatted data to a string. More secure versions of some of these functions are available; see sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l. swprintf and _swprintf_l do not have a more secure version because they take a count parameter.

 
int sprintf(
   char *buffer,
   const char *format [,
      argument] ... 
);
int _sprintf_l(
   char *buffer,
   const char *format,
   locale_t locale [,
      argument] ... 
);
int swprintf(
   wchar_t *buffer,
   size_t count,
   const wchar_t *format [,
      argument]...
);
int _swprintf_l(
   wchar_t *buffer,
   size_t count,
   const wchar_t *format,
   locale_t locale [,
      argument] ... 
);
int __swprintf_l(
   wchar_t *buffer,
   const wchar_t *format,
   locale_t locale [,
      argument] ... 
);
template <size_t size>
int sprintf(
   char (&buffer)[size],
   const char *format [,
      argument] ... 
); // C++ only
template <size_t size>
int _sprintf_l(
   char (&buffer)[size],
   const char *format,
   locale_t locale [,
      argument] ... 
); // C++ only
template <size_t size>
int swprintf(
   wchar_t (&buffer)[size],
   size_t count,
   const wchar_t *format [,
      argument]...
); // C++ only
template <size_t size>
int _swprintf_l(
   wchar_t (&buffer)[size],
   size_t count,
   const wchar_t *format,
   locale_t locale [,
      argument] ... 
); // C++ only

Collapse imageParameters

buffer

Storage location for output

count

Maximum number of characters to store in the Unicode version of this function.

format

Format-control string

argument

Optional arguments

locale

The locale to use.

For more information, see Format Specifications.

Collapse imageReturn Value

The number of characters written, or –1 if an error occurred. If buffer or format is a null pointer, the invalid parameter handler is invoked, as described in Parameter Validation. If execution is allowed to continue, these functions return -1 and set errno to EINVAL.

sprintf returns the number of bytes stored in buffer, not counting the terminating null character. swprintfreturns the number of wide characters stored in buffer, not counting the terminating null wide character.

Collapse imageRemarks

The sprintf 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. The format consists of ordinary characters and has the same form and function as the format argument for printf. A null character is appended after the last character written. If copying occurs between strings that overlap, the behavior is undefined.

Security noteSecurity Note:

Using sprintf, there is no way to limit the number of characters written, which means that code using sprintf is susceptible to buffer overruns. Consider using the related function _snprintf, which specifies a maximum number of characters to be written to buffer, or use _scprintf to determine how large a buffer is required. Also, ensure that format is not a user-defined string.

swprintf is a wide-character version of sprintf; the pointer arguments to swprintf are wide-character strings. Detection of encoding errors in swprintf may differ from that in sprintf. swprintf and fwprintf behave identically except that swprintf writes output to a string rather than to a destination of type FILE, and swprintf requires the countparameter to specify the maximum number of characters to be written. 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.

In Visual C++ 2005, swprintf conforms to the ISO C Standard, which requires the second parameter, count, of type size_t. To force the old nonstandard behavior, define _CRT_NON_CONFORMING_SWPRINTFS. In a future version, the old behavior may be removed, so code should be changed to use the new conformant behavior.

In C++, these functions have template overloads that invoke the newer, secure counterparts of these functions. For more information, see Secure Template Overloads.

TCHAR.H routine

_UNICODE & _MBCS not defined

_MBCS defined

_UNICODE defined

_stprintf

sprintf

sprintf

swprintf

_stprintf_l

_sprintf_l

_sprintf_l

__swprintf_l

Collapse imageRequirements

Routine

Required header

sprintf, _sprintf_l

<stdio.h>

swprintf, _swprintf_l

<stdio.h> or <wchar.h>

For additional compatibility information, see Compatibility in the Introduction.

Collapse imageExample

 Copy imageCopy Code
// crt_sprintf.c
// compile with: /W3
// This program uses sprintf 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( buffer,     "   String:    %s\n", s ); // C4996
   j += sprintf( buffer + j, "   Character: %c\n", c ); // C4996
   j += sprintf( buffer + j, "   Integer:   %d\n", i ); // C4996
   j += sprintf( buffer + j, "   Real:      %f\n", fp );// C4996
   // Note: sprintf is deprecated; consider using sprintf_s instead

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

character count = 79
 Copy imageCopy Code
// crt_swprintf.c
// wide character example
// also demonstrates swprintf returning error code
#include <stdio.h>

int main( void )
{
   wchar_t buf[100];
   int len = swprintf( buf, 100, L"%s", L"Hello world" );
   printf( "wrote %d characters\n", len );
   len = swprintf( buf, 100, L"%s", L"Hello\xffff world" );
   // swprintf fails because string contains WEOF (\xffff)
   printf( "wrote %d characters\n", len );
}
 Copy imageCopy Code
wrote 11 characters
wrote -1 characters
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值