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;
}

上一篇:两种多关键字排序代码


下一篇:Pulsar集群搭建部署