C++读取文件

#include <iostream>
#include <stdio.h>
#define INF 100000000000
using namespace std;

// 文件输入输出小应用
int main()
{
  FILE* fin, * fout;// 定义两个文件类型指针
  fin = fopen("data.in", "r"); //读取文件
  fout = fopen("data.out", "w"); // 写入文件
  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 %.3f", min, max, (double)s / n);
  fclose(fin);
  fclose(fout);
  return 1;
}

posted @ 2020-01-11 22:53  TyranRex  阅读(374)  评论(0编辑  收藏  举报