原文参考自:http://www.cnblogs.com/hanyonglu/archive/2011/05/07/2039916.html
1 #include <iostream> 2 #include <stdarg.h> 3 using namespace std; 4 #define COUNT 5 5 6 const char *data[COUNT] = 7 { 8 "hello", 9 "world", 10 "java", 11 "C#", 12 "Python" 13 }; 14 void fun(int a,int b,...) 15 { 16 cout<<"a="<<a<<endl; 17 cout<<"b="<<b<<endl; 18 va_list ap; 19 va_start(ap,b); 20 cout<<"defaut="<<va_arg(ap,char*)<<endl; 21 va_end(ap); 22 } 23 void Dolog(const char *szFileName,const char *fmt,...) 24 { 25 va_list ap; 26 va_start(ap,fmt); 27 FILE *fp = fopen(szFileName,"a+"); 28 if(NULL == fp) 29 { 30 return; 31 } 32 vfprintf(fp,fmt,ap); 33 va_end(ap); 34 fclose(fp); 35 } 36 37 int main() 38 { 39 int a=1,b=2; 40 char str[] = "Hello World"; 41 fun(a,b,str); 42 for(int i=0;i<COUNT;i++) 43 { 44 Dolog("log.txt","%s\n",data[i]); 45 } 46 system("pause"); 47 return 0; 48 }
log.txt内容:
hello
world
java
C#
Python