Returns the number of characters in the string referenced by a list of arguments.
int _vscprintf( const char *format, va_list argptr ); int _vscwprintf( const wchar_t *format, va_list argptr );
Parameters
-
format
- Format-control string. argptr
-
Pointer to list of arguments.
For more information, see Format Specifications.
Return Value
-
_vscprintf returns the number of characters that would be generated if the string pointed to by the list of arguments was printed or sent to a file or buffer using the specified formatting codes. The value returned does not include the terminating null character.
_vscwprintf performs the same function for wide characters.
Remarks
Each argument (if any) is converted 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.
Security Note Ensure that format is not a user-defined string. For more information, see Avoiding Buffer Overruns.
Generic-Text Routine Mappings
TCHAR.H routine | _UNICODE & _MBCS not defined | _MBCS defined | _UNICODE defined |
---|---|---|---|
_vsctprintf | _vscprintf | _vscprintf | _vswprintf |
Requirements
Routine | Required header | Compatibility |
---|---|---|
_vscprintf | <stdio.h> | ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP |
_vscwprintf | <stdio.h> or <wchar.h> | ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
All versions of the C run-time libraries.
Example
See the example for vsprintf.
string CPaiPaiHelper::Format(LPCTSTR fmt, ...)
{
string strResult="";
if (NULL != fmt)
{
va_list marker = NULL;
va_start(marker, fmt); //初始化变量参数
size_t nLength = _vscprintf(fmt, marker) + 1; //获取格式化字符串长度
std::vector<char> vBuffer(nLength, '\0'); //创建用于存储格式化字符串的字符数组
int nWritten = _vsnprintf_s(&vBuffer[0], vBuffer.size(), nLength, fmt, marker);
if (nWritten>0)
{
strResult = &vBuffer[0];
}
va_end(marker); //重置变量参数
}
return strResult;
}
转载于:https://blog.51cto.com/wellwy/491908