转换说明符%n (printf(),scanf()...)
[size=4]转换说明符%n的参数是一个int对象的地址,它向这个参数写入其所在字符串之前的字符数量.在printf(),scanf(),fprntf(),fscanf(),sprintf()中的作用相同,且不影响它们的返回值.[/size]#include<stdio.h>#include<stdlib.h>
int main(void)
{
FILE *fp;
char strinre[]="12345";
char strinwr[50];
int numa,numb,numc,numd;
scanf("%s%n",strinwr,&numa);
printf("ab%ncdef%n\n%s%n\n",&numb,&numc,strinwr,&numd);
printf("numa==%d numb==%d numc==%d numd==%d\n",numa,numb,numc,numd);
if((fp=fopen("work","w+"))==NULL)
exit(1);
fprintf(fp,"%s%nabc",strinre,&numa);
printf("%d\n",numa);
rewind(fp);
fscanf(fp,"%s%n",strinwr,&numb);
printf("%s %d\n",strinwr,numb);
return 0;
}
Welcome!
abcdef
Welcome!
numa==8 numb==2 numc==6 numd==15
5
12345abc 8
gcc 4.2.1,Digital Mars C 8.50,turbo c 2上编译成功.
VC++2005上没通过.
C89/C99上定义的标准转换说明符.
[[it] 本帖最后由 lucyqixi 于 2008-6-2 09:08 编辑 [/it]]