大家看看这个何解,fscanf还没学

记事本文件中,存放了若干实验数据(可以是真实的实验数据,也可以是假设的实验数据)。编写函数,计算出实验数据的平均数,实验数据和平均值之间的绝对差值超过0.5,就认为是无效的“脏”数据,舍弃,反之则认为是合理的数据。文件的读取由主函数完成,并输出所有合理的数据。

#include <stdio.h>
#include <math.h>
int main()
{
    FILE * fp = fopen("data.txt", "r");
    int i,count = 0;
    float a[100], sum = 0, avg;
    while (fscanf(fp, "%f", &a[count])!=EOF){
        sum += a[count];
        count++;
    }
    avg = sum / count;
    for (i = 0; i < count; i++)
    {
        if (fabs(a[i] - avg) <= 0.5)
            printf("%f ", a[i]);
    }
    return 0;
}

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img

img

可以通过该示例进行文件的读写。

/* fscanf example */
#include <stdio.h>

int main ()
{
  char str [80];
  float f;
  FILE * pFile;

  pFile = fopen ("myfile.txt","w+");
  fprintf (pFile, "%f %s", 3.1416, "PI");
  rewind (pFile);
  fscanf (pFile, "%f", &f);
  fscanf (pFile, "%s", str);
  fclose (pFile);
  printf ("I have read: %f and %s \n",f,str);
  return 0;
}

文档链接:
http://www.cplusplus.com/reference/cstdio/fscanf/