独角兽企业重金招聘Python工程师标准>>>
//numberfile.c--演示fprintf()函数的用法#include<stdio.h>#include<stdlib.h>voidclear_kb(void);intmain(void){FILE*fp;floatdata[5];intcount;charfilename[20];puts("Enter5floating-pointnumericalvalues:");for(count=0;count<5;count++)scanf("%f",&data[count]);//获得文件名,并打开文件。首先要清除stdin中的额外字符clear_kb();puts("Enteranameforthefile:");gets(filename);if((fp=fopen(filename,"w"))==NULL){fprintf(stderr,"Erroropeningfile%s.",filename);exit(1);}for(count=0;count<5;count++){//将数值输出到磁盘文件fprintf(fp,"\ndata[%d]=%f",count,data[count]);//将数值输出到流fprintf(stdout,"\ndata[%d]=%f",count,data[count]);}fclose(fp);printf("\n");return0;}//清理stdin中的其他字符voidclear_kb(void){charjunk[80];gets(junk);}
如果觉得《使用fprintf()函数格式化输出至磁盘文件和是stdout》对你有帮助,请点赞、收藏,并留下你的观点哦!