c - 简单的数学运算,将值从文件读入数组

标签 c arrays file

#include <stdio.h>
int main(void)
{   
int file, i, total, min, max, num;
float avg;
int scores[1000];
int morescores[1000];
min = 10000000;
max = -10000000;


FILE *afile;
afile = fopen("scores.txt", "r");

i=0;
while(fscanf(afile, "%d", &num) != EOF) {
    i++;
    }
    printf("The number of values in scores.txt is %d\n", i);

//so we know there are 35 values in this file

fclose(afile);


afile = fopen("scores.txt", "r"); 
i=total=0;
while(fscanf(afile, "%d", &scores[i]) != EOF) {
    i++;
    total += scores[i];

    avg = total/i;

    if (scores[i] < min) {
    min = scores[i];
    } else if (scores[i] > max) {
    max = scores[i];
    }
}
    printf("The total of the integers is %d.\n", total);
    printf("The number of integers in the file is %d.\n", i);
    printf("The average of the integers is %f.\n", avg);
    printf ("The minimum is %d.\n", min);
    printf ("The maximum is %d.\n", max);       


    fclose(afile);
   return (0);
}

我正在尝试从文件 scores.txt 中读取所有值,并使用这些值进行数学表达式。在对文件进行数学运算时,我不知道如何调用文件中的特定值。当我将 scores[i] 放入表达式中时,它不起作用。有什么建议吗?

最佳答案

你必须移动i的增量

while(fscanf(afile, "%d", &scores[i]) != EOF) {
   i++;

在循环结束时:

while(fscanf(afile, "%d", &scores[i]) != EOF) {
   ...
   i++;
}

因为您在 scores[n] 中存储了一个值并使用 scores[n+1]...

您的代码变为:

#include <stdio.h>

int main(void) {
   int    num;
   float  avg;
   int    scores[1000];
   int    morescores[1000];
   int    min = 10000000;
   int    max = -10000000;
   FILE * afile = fopen("scores.txt", "r");
   if( ! afile ) {
      perror("scores.txt");
      return 1;
   }
   int    i = 0;
   while( fscanf( afile, "%d", &num ) != EOF ) {
      i++;
   }
   printf("The number of values in scores.txt is %d\n", i);
   fclose(afile);
   afile = fopen("scores.txt", "r");
   int total = 0;
   i = 0;
   while( fscanf( afile, "%d", &(scores[i])) != EOF) {
      total += scores[i];
      if (scores[i] < min) {
        min = scores[i];
      }
      else if (scores[i] > max) {
        max = scores[i];
      }
      i++;
   }
   avg = total/i;
   printf("The total of the integers is %d.\n", total);
   printf("The number of integers in the file is %d.\n", i);
   printf("The average of the integers is %f.\n", avg);
   printf("The minimum is %d.\n", min);
   printf("The maximum is %d.\n", max);
   fclose( afile );
   return 0;
}

执行变成:

aubin@Breizh-Atao ~/Dev/C $ echo 1 2 3 4 5 6 7 8 9 10 > scores.txt
aubin@Breizh-Atao ~/Dev/C $ gcc minMax.c -o MinMax
aubin@Breizh-Atao ~/Dev/C $ ./MinMax 
The number of values in scores.txt is 10
The total of the integers is 55.
The number of integers in the file is 10.
The average of the integers is 5.000000.
The minimum is 1.
The maximum is 10.
aubin@Breizh-Atao ~/Dev/C $ 

关于c - 简单的数学运算,将值从文件读入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42055847/

相关文章:

C程序跑不起来——初学者对指针&字符的疑问

php - 无法使用 PHP 创建的管道写入 C 应用程序的 STDIN

c - 从C中的指针检索二维数组

vb.net - 将文件转换为base64函数输出

azure - 如何使用 ADF 检查和比较文件夹 (Datalake) 内的文件名

通过内联汇编访问系统时间后出现 C 段错误

java - Jackson JSON - 从嵌套数组中提取数据

php - php中的数组,比较2个数组并保留重复值

c++ - Memset 枚举数组值设置不正确 (C/C++)

java - 如何将我的 txt 文件转换为字符串