标签:blog http io ar 使用 sp java for 文件
实现可变长度将用到以下这些宏:
void va_start( va_list arg_ptr, prev_param );
type va_arg( va_list arg_ptr, type );
void va_end( va_list arg_ptr );
va在这里是variable-argument(可变参数)的意思.
这些宏定义在stdarg.h中,所以用到可变参数的程序应该包含这个头文件。
实现步骤如下:
/* Writen by samdy1990 at 1st December 2014 in SICT CNC lab reference:http://www.cnhonkerarmy.com/blog-719-654.html */ #include <stdio.h> #include <stdarg.h>//must include the head file. #define FORMATLEN 100 int miniscanf(char * format,...) { va_list ap; va_start(ap,format);// make ap point to 1st unnamed arg int *ival; unsigned *uval; double *dval; char *sval,*p int i = 0; char *localfmt[FORMATLEN]; //loop for(p=format; *p;p++)//to read the format string. { if(*p != '%') { localfmt[i++] = *p; continue; } else { localfmt[i++] = '%'; while(*(p+1)&&!isalpha(*(p+1))) { localfmt[i++] = *++p; } localfmt[i++] = *(p+1); loaclfmt[i] = '\0'; switch (*++p) { case 'd': case 'i': ival = va_arg(ap,int *); scanf(localfmt,ival); break; case 'x': case 'X': case 'o': case 'u': uval = va_arg(ap,unsigned *); scanf(localfmt,uval); break; case 'g': case 'e': case 'f': dval = va_arg(ap,double *); scanf(localfmt,dval); break; case 's': sval = va_arg(ap,char *); scanf(localfmt,sval); break; default: scanf(localfmt); break; } i=0; } } val_end(ap); }
标签:blog http io ar 使用 sp java for 文件
原文地址:http://blog.csdn.net/samdy1990/article/details/41647753