C语言程序设计第四版(何钦铭、颜晖)第十二章文件之实数取整写入文件

张开发
2026/4/12 16:56:34 15 分钟阅读

分享文章

C语言程序设计第四版(何钦铭、颜晖)第十二章文件之实数取整写入文件
【练习12-6】实数取整写入文件:文件f1.txt中有若干个实数,请分别读出,将每个实数按四舍五入取整后存入文件f2.txt中。试编写相应程序。#includestdio.h #includestdlib.h #includemath.h int main(){ double num; int integer_num; FILE *fp1fopen(f1.txt,r); if(fp1NULL){ printf(File(f1.txt) can not open!\n); exit(0); } FILE *fp2fopen(f2.txt,w); if(fp2NULL){ printf(File(f2.txt) can not open!\n); exit(0); } while(fscanf(fp1,%lf,num)1){ integer_num(int)round(num); fprintf(fp2,%d ,integer_num); } if(fclose(fp1)){ printf(Can not close the file(f1.txt)!\n); exit(0); } if(fclose(fp2)){ printf(Can not close the file(f2.txt)!\n); exit(0); } return 0; }

更多文章