vsprintf, vswprintf與printf與函數(shù)的可變參數(shù)編程
Stephen Liu
在C語言編程中,我們不可避免的要接觸到可變參數(shù)函數(shù),對于不支持函數(shù)多態(tài)的C語言來講,使用可變參數(shù)和宏定義函數(shù)是變通的實(shí)現(xiàn)函數(shù)多態(tài)的好方法。在進(jìn)一步涉及到可變參數(shù)函數(shù)之前,我們先來看看常用到的兩個(gè)可變參數(shù)的典型,分別是vsprintf和sprintf。
一、vsprintf函數(shù)
Header File
stdio.h
Category
Memory and String Manipulation Routines
Prototype
int vsprintf(char *buffer, const char *format, va_list arglist);
int vswprintf(wchar_t *buffer, const wchar_t *format, va_list arglist);
Description
Writes formatted output to a string.
The v...printf functions are known as alternate entry points for the ...printf functions. They behave exactly like their ...printf counterparts, but they accept a pointer to a list of arguments instead of an argument list.
vsprintf accepts a pointer to a series of arguments, applies to each a format specifier contained in the format string pointed to by format, and outputs the formatted data to a string. There must be the same number of format specifiers as arguments.
Return Value
vsprintf returns the number of bytes output. In the event of error, vsprintf returns EOF.
--對照翻譯
頭文件
stdio.h
分類
內(nèi)存和字符串操作
函數(shù)原型
int vsprintf(char *buffer, const char *format, va_list arglist);
int vswprintf(wchar_t *buffer, const wchar_t *format, va_list arglist);
描述
寫格式化后的輸出到一個(gè)字符串
v..printf函數(shù)族是..print函數(shù)族的可替代函數(shù),他們像..printf函數(shù)族一樣操作,但是他們接受指向參數(shù)列表的指針而不是參數(shù)列表。
vsprintf接受一個(gè)指向一系列可變參數(shù)的指針,提供給每一個(gè)參數(shù)一個(gè)包含在form中的格式化定義,并且輸出格式化后的數(shù)據(jù)到一個(gè)字符串中,格式定義和參數(shù)數(shù)量必須相等。
返回值
vsprintf返回輸出的字節(jié)數(shù)目,出錯(cuò)時(shí)返回EOF
二、sprintf函數(shù)
Header File
stdio.h
Category
Memory and String Manipulation Routines
Prototype
int sprintf(char *buffer, const char *format[, argument, ...]);
int swprintf(wchar_t *buffer, const wchar_t *format[, argument, ...]);
Description
Writes formatted output to a string.
Note:?For details on format specifiers, see printf.
sprintf accepts a series of arguments, applies to each a format specifier contained in the format string pointed to by format, and outputs the formatted data to a string.
sprintf applies the first format specifier to the first argument, the second to the second, and so on. There must be the same number of format specifiers as arguments.
Return Value
On success, sprintf returns the number of bytes output. The return value does not include the terminating null byte in the count.
On error, sprintf returns EOF.
--對照翻譯
頭文件:
stdio.h
頭文件
stdio.h
分類
內(nèi)存和字符串操作
函數(shù)原型
int sprintf(char *buffer, const char *format[, argument, ...]);
int swprintf(wchar_t *buffer, const wchar_t *format[, argument, ...]);
描述
寫格式化后的輸出到一個(gè)字符串
注意:對于格式化定義規(guī)范,參看printf
sprintf接受一系列參數(shù),提供給每一個(gè)參數(shù)一個(gè)格式化定義,并且輸出格式化數(shù)據(jù)到字符串
sprintf提供給首個(gè)參數(shù)第一個(gè)格式化定義,第二個(gè)賦予次個(gè)格式化定義,格式化定義數(shù)量必須和參數(shù)數(shù)量一致
返回值
成功,返回輸出的字節(jié)數(shù)量,返回值不包含終止null字節(jié)的字節(jié)數(shù)量
錯(cuò)誤,返回EOF
?
為了便于比較這兩個(gè)函數(shù)的使用,下面給出一個(gè)程序片段:
??? char szBuffer[256];
??? sprintf(szBuffer, "welcome %d, %s", 1, "hi");
??? ShowMessage(szBuffer);
??? vsprintf(szBuffer, "welcome %d, %s", 1, "hi"); //<-提示[C++ Error] Unit1.cpp(24): E2034 Cannot convert 'int' to 'void *'
??? ShowMessage(szBuffer);
?
?