[MSDN]sprintf, swprintf

5690 ワード

sprintf,swprintfsprintf,swprintf関数


Write formatted data to a string.パラメータを指定したフォーマットでバッファに出力
int sprintf( char *buffer, const char *format [, argument] ... );
int swprintf( wchar_t *buffer, const wchar_t *format [, argument] ... );
Routine
Required Header
Compatibility
sprintf

ANSI, Win 95, Win NT
swprintf
or
ANSI, Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB
Single thread static library, retail version
LIBCMT.LIB
Multithread static library, retail version
MSVCRT.LIB
Import library for MSVCRT.DLL, retail version
Return Value戻り値
sprintf returns the number of bytes stored in buffer, not counting the terminating null character. sprintfはbuffer文字列の格納バイト数を返し、終端を含まない.swprintf returns the number of wide characters stored in buffer, not counting the terminating null wide character.swprintfはbufferの幅を返します.
Parameters
buffer
Storage location for output
format
Format-control string
argument
Optional arguments
For more information, see Format Specifications .
Remarks
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.
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.
Generic-Text Routine Mappings
TCHAR.H Routine
_UNICODE & _MBCS Not Defined
_MBCS Defined
_UNICODE Defined
_stprintf
sprintf
sprintf
swprintf
Example
/* SPRINTF.C: This program uses sprintf to format various

 * data and place them in the string named buffer.

 */



#include <stdio.h>



void 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,     "\tString:    %s
", s );   
   // :2,"String:”:7,"computer":8, 4 : :21   j += sprintf( buffer + j, "\tCharacter: %c
", c );   j += sprintf( buffer + j, "\tInteger:   %d
", i );   j += sprintf( buffer + j, "\tReal:      %f
", fp );   printf( "Output:
%s
character count = %d
", buffer, j ); }

Output
Output:

   String:    computer

   Character: l

   Integer:   35

   Real:      1.732053



character count = 71




Stream I/O Routines
See Also    _snprintf , fprintf , printf , scanf , sscanf , vprintf Functions


Send feedback to MSDN.

Look here
for MSDN Online resources.