请输入您要查询的百科知识:

 

词条 sprintf_s
释义

函数简介

函数功能:将数据格式化输出到字符串

函数原型:

intsprintf_s(

char *buffer,

size_tsizeOfBuffer,

constchar *format [,

argument] ...

);

头文件:stdio.h

相关函数:

int sprintf( char *buffer, const char *format [, argument] … );

int vsprintf( char *buffer, const char *format, va_list argptr );

int _sprintf_s_l(

char *buffer,

size_t sizeOfBuffer,

const char *format,

locale_t locale [,

argument] ...

);

int swprintf_s(

wchar_t*buffer,

size_t sizeOfBuffer,

const wchar_t *format [,

argument]...

);

int _swprintf_s_l(

wchar_t *buffer,

size_t sizeOfBuffer,

const wchar_t *format,

locale_t locale [,

argument]…

);

template <size_t size>

int sprintf_s(

char (&buffer)[size],

const char *format [,

argument] ...

); // C++ only

template <size_t size>

int swprintf_s(

wchar_t (&buffer)[size],

const wchar_t *format [,

argument]...

); // C++ only

备注:

sprintf_s()是sprintf()的安全版本,通过指定缓冲区长度来避免sprintf()存在的溢出风险

程序示例

// 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\", s );

j += sprintf_s( buffer + j, 200 - j, " Character: %c\", c );

j += sprintf_s( buffer + j, 200 - j, " Integer: %d\", i );

j += sprintf_s( buffer + j, 200 - j, " Real: %f\", fp );

printf_s( "Output:\%s\character count = %d\", buffer, j );

}

Output:

String: computer

Character: l

Integer: 35

Real: 1.732053

character count = 79

随便看

 

百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2024/11/15 12:33:33