#include <stdio.h>
#define INF 1000000000
int main(){
FILE *fin,*fout;
fin=fopen("data.in","rb");
fout=fopen("data.out","wb");
int x,n=0,min=INF,max=-INF,s=0;
while(fscanf(fin,"%d",&x)==1){
s+=x;
if(x<min){
min=x;
}
if(x>max){
max=x;
}
n++;
}
fprintf(fout,"%d %d %.31f\n",min,max,(double)s/n);
fclose(fin);
fclose(fout);
return 0;
}
fopen与重定向的方法比较:重定向简单但是不能同时读写文件和标准输入输出;
但是fopen写法复杂但是灵活性大(可以反复打开并读写文件);而且如果想把fopen版的程序改成标准输入输出,值需赋值fin=stdin;fout=stout即可;不要调用fopen和fclose(因为这个方法在Linux下是无效的)它不具有可移植性