如何将一个txt文件中的一系列数 读入数组,并输出txt文件中的最小值,负数的个数和 所有偶数的和,
比如txt文件中的数是
2
-6
9
8
这样的
#include <limits.h>
#include <stdio.h>
int main()
{
int min = INT_MAX, count = 0, sum = 0, t;
FILE *fp;
fp = fopen("mydata.txt", "r");
if (fp == NULL)
{
printf("open file Error!\n");
return 1;
}
while (!feof(fp))
{
fscanf(fp, "%d", &t);
if (t < 0)
count++;
if (t < min)
min = t;
if (t % 2 == 0)
sum += t;
}
fclose(fp);
printf("%d %d %d\n", min, count, sum);
return 0;
}