正在加载图片...
你现在所处的位置在:首页→C语言教学→幻灯片版→第十章文件 ●格式化o: fprint与 fscanf ●函数原# nclude< stdio h> nt fprint maino int fscan i char s[80] c[80] a 功能:找FIE 返值:成:6(p=fope"ewy)NUL) 例 fprintf(fp i puts("can't open file"); exito; fscanf(fp, o fscanf(stdin, " %s%d,s, &a) *read from keaboard=*/ fprintf(fp %s %", S, a) *write to file*/ 例从键盘按格fl0se( if(fp=fopen("test"," I==NULL) i puts("can't open file"); exit(; ChIz 70.c fscanf(fp, %S%d",c, &b); /*read from file*/ fprintf( stdout, %s%/,c, b); /*print to screen*/ fclose(fp)你现在所处的位置在:首页→C语言教学→幻灯片版→第十章 文件 14 ⚫ 格式化I/O:fprintf与fscanf ⚫ 函数原型: int fprintf(FILE *fp,const char *format[,argument,…]) int fscanf(FILE *fp,const char *format[,address,…]) 功能:按格式对文件进行I/O操作 返值:成功,返回I/O的个数;出错或文件尾,返回EOF 例 fprintf(fp,“%d,%6.2f”,i,t); //将i和t按%d,%6.2f格式输出到fp文件 fscanf(fp,“%d,%f”,&i,&t); //若文件中有3,4.5 ,则将3送入i, 4.5送入t 例 从键盘按格式输入数据存到磁盘文件中去 #include <stdio.h> main() { char s[80],c[80]; int a,b; FILE *fp; if((fp=fopen("test","w"))==NULL) { puts("can't open file"); exit() ; } fscanf(stdin,"%s%d",s,&a);/*read from keaboard*/ fprintf(fp,"%s %d",s,a);/*write to file*/ fclose(fp); if((fp=fopen("test","r"))==NULL) { puts("can't open file"); exit(); } fscanf(fp,"%s%d",c,&b);/*read from file*/ fprintf(stdout,"%s %d",c,b);/*print to screen*/ fclose(fp); }
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有