user-0 提问: 神奇的sprintf格式

回复:
user: user-1
content: 仅供参考：#include <conio.h> #include <math.h> #include <stdio.h> #include <string.h> FILE *f; int i,n,line,st; int n1,n2,n3; int b1,b2,b3; int e1,e2,e3; int w1,w2,w3; int v1,v2,v3; char ln[256]; char fmt[256]; char fmd[6]; char tmp[256]; char *s1,*s2,*s3,*s4; char L,R,D; void main(int argc,char *argv[]) {     if (argc<2) {     PMT:         printf("%s filename.ext [ParenthesesChar] [DelimiterChar]\nParenthesesChar is '(' default or '[' or '{'\nDelimiterChar is ',' default",argv[0]);         return;     }     if (argc==2) {         L='(';R=')';D=',';     } else {         switch (argv[2][0]) {             case '(':L='(';R=')';break;             case '[':L='[';R=']';break;             case '{':L='{';R='}';break;             default:goto PMT;         }         D=',';         if (argc>=4) D=argv[3][0];     }     sprintf(fmd,"%%d%c%%d",D);     if ((f=fopen(argv[1],"rt"))==NULL) {         printf("Can not open %s",argv[1]);         return;     }     line=0;     while (1) {         fgets(ln,255,f);         if (feof(f)) break;//         line++;         n=0;st=0;         for (i=0;i<(int)strlen(ln);i++) {             switch (st) {             case 0:if (ln[i]==L) st=1;break;             case 1:if (ln[i]==D) st=2;break;             case 2:if (ln[i]==R) {st=0;n++;} break;             }         }         switch (n) {             case 0:                 printf("%s",ln);             break;             case 1:                 s1=strchr(ln,L);                 s2=strchr(s1,R);                 if (s2==NULL) {                     cprintf("\a\a\a missing %c in line %d",R,line);                     fclose(f);                     return;                 }                 s1[0]=0;                 s1++;                 s2++;                 v1=sscanf(s1,fmd,&b1,&e1);                 if (v1==2) {                     w1=1+(int)log10((double)e1);                     sprintf(fmt,"%%s%%0%1dd%%s",w1);                     for (n1=b1;n1<=e1;n1++) {                         printf(fmt,ln,n1,s2);                     }                 } else if (s1[1]==D) {                     b1=s1[0];                     e1=s1[2];                     for (n1=b1;n1<=e1;n1++) {                         printf("%s%c%s",ln,n1,s2);                     }                 } else {                     printf("%s",ln);                 }             break;             case 2:                 s1=strchr(ln,L);                 s2=strchr(s1,R);                 if (s2==NULL) {                     cprintf("\a\a\a missing %c in line %d",R,line);                     fclose(f);                     return;                 }                 s1[0]=0;                 s1++;                 s2++;                 v1=sscanf(s1,fmd,&b1,&e1);                 if (v1==2) {                     w1=1+(int)log10((double)e1);                 } else {                     b1=s1[0];                     e1=s1[2];                 }                  s1=strchr(s2,L);                 s3=strchr(s1,R);                 if (s3==NULL) {                     cprintf("\a\a\a missing %c in line %d",R,line);                     fclose(f);                     return;                 }                 s1[0]=0;                 s1++;                 s3++;                 v2=sscanf(s1,fmd,&b2,&e2);                 if (v2==2) {                     w2=1+(int)log10((double)e2);                 } else {                     b2=s1[0];                     e2=s1[2];                 }                  strcpy(fmt,"%s");                 if (v1==2) sprintf(tmp,"%%0%1dd",w1);                 else strcpy(tmp,"%c");                 strcat(fmt,tmp);                 strcat(fmt,"%s");                 if (v2==2) sprintf(tmp,"%%0%1dd",w2);                 else strcpy(tmp,"%c");                 strcat(fmt,tmp);                 strcat(fmt,"%s");                 for (n1=b1;n1<=e1;n1++) {                     for (n2=b2;n2<=e2;n2++) {                         printf(fmt,ln,n1,s2,n2,s3);                     }                 }             break;             case 3:                 s1=strchr(ln,L);                 s2=strchr(s1,R);                 if (s2==NULL) {                     cprintf("\a\a\a missing %c in line %d",R,line);                     fclose(f);                     return;                 }                 s1[0]=0;                 s1++;                 s2++;                 v1=sscanf(s1,fmd,&b1,&e1);                 if (v1==2) {                     w1=1+(int)log10((double)e1);                 } else {                     b1=s1[0];                     e1=s1[2];                 }                  s1=strchr(s2,L);                 s3=strchr(s1,R);                 if (s3==NULL) {                     cprintf("\a\a\a missing %c in line %d",R,line);                     fclose(f);                     return;                 }                 s1[0]=0;                 s1++;                 s3++;                 v2=sscanf(s1,fmd,&b2,&e2);                 if (v2==2) {                     w2=1+(int)log10((double)e2);                 } else {                     b2=s1[0];                     e2=s1[2];                 }                  s1=strchr(s3,L);                 s4=strchr(s1,R);                 if (s4==NULL) {                     cprintf("\a\a\a missing %c in line %d",R,line);                     fclose(f);                     return;                 }                 s1[0]=0;                 s1++;                 s4++;                 v3=sscanf(s1,fmd,&b3,&e3);                 if (v3==2) {                     w3=1+(int)log10((double)e3);                 } else {                     b3=s1[0];                     e3=s1[2];                 }                  sprintf(fmt,"%%s%%0%1dd%%s%%0%1dd%%s%%0%1dd%%s",w1,w2,w3);                 strcpy(fmt,"%s");                 if (v1==2) sprintf(tmp,"%%0%1dd",w1);                 else strcpy(tmp,"%c");                 strcat(fmt,tmp);                 strcat(fmt,"%s");                 if (v2==2) sprintf(tmp,"%%0%1dd",w2);                 else strcpy(tmp,"%c");                 strcat(fmt,tmp);                 strcat(fmt,"%s");                 if (v3==2) sprintf(tmp,"%%0%1dd",w3);                 else strcpy(tmp,"%c");                 strcat(fmt,tmp);                 strcat(fmt,"%s");                  for (n1=b1;n1<=e1;n1++) {                     for (n2=b2;n2<=e2;n2++) {                         for (n3=b3;n3<=e3;n3++) {                             printf(fmt,ln,n1,s2,n2,s3,n3,s4);                         }                      }                 }             break;             default:                 cprintf("\a\a\a > 3 repeat in line %d",line);                 fclose(f);                 return;         }     }     fclose(f); }

user: user-2
content: 两个%是作为一个%字符理解，你这个相当于是用level*4替换了最前面的那个 %d

user: user-2
content: http://www.cplusplus.com/reference/cstdio/sprintf/?kw=sprintf

user: user-3
content: 比如，当 level 为2时，fmt 内容成了 "%-8s%s,  get:%p set:%p refresh:%p" 到后来用它当做函数 cwmp_log_debug 的参数，跟 printf 的第一个参数作用差不多