首先看一下sprintf_S函数:
int sprintf_s( char *buffer, size_t sizeOfBuffer, const char *format [, argument] ... );
这个函数的主要作用是将若干个argument按照format格式存到buffer中,其中
buffer
输出的字符
sizeOfBuffer
buffer的长度
format
格式字符串,比如%s
argument
可选参数
再看_stprintf_s:
网上查到有言:
前面的t表示编码
后面的_s表示检查内存溢出
前面的_表示非标准库函数
注:转自http://bbs.xml.org.cn/dispbbs.asp?BoardID=61&id=56706&star=1
在2005编译以前的VC++项目时,经常出现与字符串处理相关的一堆错误,有UNICODE和ASCII 两种不同的编码方式,不同的编码方式要使用不同的字符串处理函数,也就是一种是char,8位的;一个种wchar_t,16位的.以前VC++6中也有这个问题,在VS2005中编译的时候,总是一堆错误,最多的就是与字符串处理相关,
在VS2005中,还是要严格的使用保险的带t的类型,
下面是一些风格字符串对应的t形式的宏在\Microsoft Visual Studio 8\VC\include\tchar.h中进行定义的
/* Formatted i/o */
#define _tprintf printf
#define _tprintf_l _printf_l
#define _tprintf_s printf_s
#define _tprintf_s_l _printf_s_l
#define _tprintf_p _printf_p
#define _tprintf_p_l _printf_p_l
#define _tcprintf _cprintf
#define _tcprintf_l _cprintf_l
#define _tcprintf_s _cprintf_s
#define _tcprintf_s_l _cprintf_s_l
#define _tcprintf_p _cprintf_p
#define _tcprintf_p_l _cprintf_p_l
#define _vtcprintf _vcprintf
#define _vtcprintf_l _vcprintf_l
#define _vtcprintf_s _vcprintf_s
#define _vtcprintf_s_l _vcprintf_s_l
#define _vtcprintf_p _vcprintf_p
#define _vtcprintf_p_l _vcprintf_p_l
#define _ftprintf fprintf
#define _ftprintf_l _fprintf_l
#define _ftprintf_s fprintf_s
#define _ftprintf_s_l _fprintf_s_l
#define _ftprintf_p _fprintf_p
#define _ftprintf_p_l _fprintf_p_l
#define _stprintf sprintf
#define _stprintf_l _sprintf_l
#define _stprintf_s sprintf_s
#define _stprintf_s_l _sprintf_s_l
#define _stprintf_p _sprintf_p
#define _stprintf_p_l _sprintf_p_l
#define _sctprintf _scprintf
#define _sctprintf_l _scprintf_l
#define _sctprintf_p _scprintf_p
#define _sctprintf_p_l _scprintf_p_l
#define _sntprintf _snprintf
#define _sntprintf_l _snprintf_l
#define _sntprintf_s _snprintf_s
#define _sntprintf_s_l _snprintf_s_l
#define _vtprintf vprintf
#define _vtprintf_l _vprintf_l
#define _vtprintf_s vprintf_s
#define _vtprintf_s_l _vprintf_s_l
#define _vtprintf_p _vprintf_p
#define _vtprintf_p_l _vprintf_p_l
#define _vftprintf vfprintf
#define _vftprintf_l _vfprintf_l
#define _vftprintf_s vfprintf_s
#define _vftprintf_s_l _vfprintf_s_l
#define _vftprintf_p _vfprintf_p
#define _vftprintf_p_l _vfprintf_p_l
#define _vstprintf vsprintf
#define _vstprintf_l _vsprintf_l
#define _vstprintf_s vsprintf_s
#define _vstprintf_s_l _vsprintf_s_l
#define _vstprintf_p _vsprintf_p
#define _vstprintf_p_l _vsprintf_p_l
#define _vsctprintf _vscprintf
#define _vsctprintf_l _vscprintf_l
#define _vsctprintf_p _vscprintf_p
#define _vsctprintf_p_l _vscprintf_p_l
#define _vsntprintf _vsnprintf
#define _vsntprintf_l _vsnprintf_l
#define _vsntprintf_s _vsnprintf_s
#define _vsntprintf_s_l _vsnprintf_s_l
#define _tscanf scanf
#define _tscanf_l _scanf_l
#define _tscanf_s scanf_s
#define _tscanf_s_l _scanf_s_l
#define _tcscanf _cscanf
#define _tcscanf_l _cscanf_l
#define _tcscanf_s _cscanf_s
#define _tcscanf_s_l _cscanf_s_l
#define _ftscanf fscanf
#define _ftscanf_l _fscanf_l
#define _ftscanf_s fscanf_s
#define _ftscanf_s_l _fscanf_s_l
#define _stscanf sscanf
#define _stscanf_l _sscanf_l
#define _stscanf_s sscanf_s
#define _stscanf_s_l _sscanf_s_l
#define _sntscanf _snscanf
#define _sntscanf_l _snscanf_l
#define _sntscanf_s _snscanf_s
#define _sntscanf_s_l _snscanf_s_l
/* Unformatted i/o */
#define _fgettc fgetc
#define _fgettc_nolock _fgetc_nolock
#define _fgettchar _fgetchar
#define _fgetts fgets
#define _fputtc fputc
#define _fputtc_nolock _fputc_nolock
#define _fputtchar _fputchar
#define _fputts fputs
#define _cputts _cputs
#define _gettc getc
#define _gettc_nolock _getc_nolock
#define _gettch _getch
#define _gettch_nolock _getch_nolock
#define _gettche _getche
#define _gettche_nolock _getche_nolock
#define _gettchar getchar
#define _gettchar_nolock _getchar_nolock
#define _getts gets
#define _getts_s gets_s
#define _cgetts _cgets
#define _cgetts_s _cgets_s
#define _puttc putc
#define _puttc_nolock _putc_nolock
#define _puttchar putchar
#define _puttchar_nolock _putchar_nolock
#define _puttch _putch
#define _puttch_nolock _putch_nolock
#define _putts puts
#define _ungettc ungetc
#define _ungettc_nolock _ungetc_nolock
#define _ungettch _ungetch
#define _ungettch_nolock _ungetch_nolock
/* String conversion functions */
#define _tcstod strtod
#define _tcstol strtol
#define _tcstoul strtoul
#define _tstof atof
#define _tstol atol
#define _tstoi atoi
#define _tstoi64 _atoi64
#define _tcstod_l _strtod_l
#define _tcstol_l _strtol_l
#define _tcstoul_l _strtoul_l
#define _tstof_l _atof_l
#define _tstol_l _atol_l
#define _tstoi_l _atoi_l
#define _tstoi64_l _atoi64_l
#define _itot_s _itoa_s
#define _ltot_s _ltoa_s
#define _ultot_s _ultoa_s
#define _itot _itoa
#define _ltot _ltoa
#define _ultot _ultoa
#define _ttoi atoi
#define _ttol atol
#define _ttoi64 _atoi64
#define _tcstoi64 _strtoi64
#define _tcstoi64_l _strtoi64_l
#define _tcstoui64 _strtoui64
#define _tcstoui64_l _strtoui64_l
#define _i64tot_s _i64toa_s
#define _ui64tot_s _ui64toa_s
#define _i64tot _i64toa
#define _ui64tot _ui64toa
/* String functions */
/* Note that _mbscat, _mbscpy and _mbsdup are functionally equivalent to
strcat, strcpy and strdup, respectively. */
#define _tcscat strcat
#define _tcscat_s strcat_s
#define _tcscpy strcpy
#define _tcscpy_s strcpy_s
#define _tcsdup _strdup
#define _tcslen strlen
#define _tcsnlen strnlen
#define _tcsxfrm strxfrm
#define _tcsxfrm_l _strxfrm_l
#define _tcserror strerror
#define _tcserror_s strerror_s
#define __tcserror _strerror
#define __tcserror_s _strerror_s