Arrays 如何平均一个文件中的数字,并使用C将结果放回一个新文件中

Arrays 如何平均一个文件中的数字,并使用C将结果放回一个新文件中,arrays,c,sorting,average,Arrays,C,Sorting,Average,我正在编写一个程序,它将从一个名为numbers.txt的文件中获取10个数字,并将它们放入一个数组中,求平均值,然后将平均值写回该文件。我已经创建并存储了名为numbers.txt的文件。当我运行代码时,它不会处理,我也不知道为什么。我对编码和慢慢学习很陌生。有人能帮我查一下这个代码吗 谢谢 这是我的密码: #include <stdio.h> #include <math.h> int main(int argc, char* argv[]){ in

我正在编写一个程序,它将从一个名为numbers.txt的文件中获取10个数字,并将它们放入一个数组中,求平均值,然后将平均值写回该文件。我已经创建并存储了名为numbers.txt的文件。当我运行代码时,它不会处理,我也不知道为什么。我对编码和慢慢学习很陌生。有人能帮我查一下这个代码吗

谢谢

这是我的密码:

#include <stdio.h>
#include <math.h>
    
int main(int argc, char* argv[]){
    int i = 0, sum = 0, n = 0,avg = 0;
    FILE *numbers.txt;
    fin = fopen("numbers.txt", "r");
    while(fscanf(numbers.txt, "%d", &n) != EOF){
        sum += n;
        i++;
        avg = (sum / i);
    }
    printf("The average is %d.\n", avg);
    fclose(numbers.txt);
    return 0;
}
#包括
#包括
int main(int argc,char*argv[]){
int i=0,sum=0,n=0,avg=0;
文件*numbers.txt;
fin=fopen(“numbers.txt”、“r”);
while(fscanf(numbers.txt、%d、&n)!=EOF){
总和+=n;
i++;
平均值=(总和/i);
}
printf(“平均值为%d.\n”,平均值);
fclose(numbers.txt);
返回0;
}

调用
fscanf
,第一个参数为
numbers.txt
(与
close
相同)。这是无效的,
numbers.txt
不是有效的符号名

您将
打开
,分配给
fin
。将
numbers.txt
替换为
fin
,字符串中除外。变量名中不能有
。您显示的内容甚至无法编译


您还应该检查
fscanf
的结果,以了解
EOF
以外的内容。您知道何时到达文件末尾,但不知道是否已成功读入值。输入成功时,它应为1


根据
fscanf

上的文档,根据文件的结构,您可能希望在格式字符串的前面添加一个空格字符,或者对其进行一些其他更改。“它不处理”不是一个非常有用的描述-您需要确切地告诉我们您看到了什么。如果编译器在抱怨,那么您需要在问题主体中包含编译器错误。如果代码已编译但未正确运行,您需要告诉我们您预期会发生什么以及实际发生的情况(例如,“我希望看到这个特定的输出,但我没有得到任何输出/我得到了垃圾/我得到了一个我不期望的值”,等等)

如果您发布的内容是您实际尝试运行的内容,那么您可能会在变量
numbers.txt
上遇到编译器错误。
不是标识符1的有效字符,因此编译器将拒绝类似
FILE*numbers.txt
的声明

我擅自修改了您的代码并对其进行了注释-希望这将有助于:

/**
 * While the compiler couldn't care less about how you format your code,
 * it's a good idea to use indentation and whitespace to make your code
 * easier for other people to read.  Exact styles and preferences vary,
 * what matters is that you're consistent.  This is the style I prefer.
 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(int argc, char* argv[])
{
  int i = 0, sum = 0, n = 0,avg = 0;
  /**
   * numbers.txt is not a valid identifier - you can't use that as a variable
   * name.  I'm going to assume you meant to use fin as that's the target of
   * your fopen call.  Note that you can initialize the variable with the
   * result of the function call.  
   */
  FILE *fin = fopen("numbers.txt", "r");

  /**
   * It's always a good idea to make sure the fopen call succeeded before
   * trying to use the file pointer.
   */
  if ( !fin ) // or fin == NULL 
  {
    fputs( "Could not open numbers.txt", stderr );
    exit( EXIT_FAILURE );
  }

  /**
   * With the *scanf functions, it's better to check to see if you
   * got the expected number of inputs rather than checking for EOF.
   * It's possible for *scanf to return 0 if the first input character
   * doesn't match the format specifier - e.g., if you read an 'a' or a 
   * '.' or something like that.  
   * 
   * In this case we expect 1 integer to be read, so we check that fscanf
   * returns 1.  If it doesn't, *then* we check for EOF or bad input.  We'll
   * create a new variable named items_read to store the return value
   * of fscanf.  
   *
   * For this example, we'll bail out immediately if we see bad input.  
   * There are ways to recover from bad input, but they'll make this 
   * example longer than it needs to be.
   */
  int items_read;
  while( (items_read = fscanf(fin, "%d", &n)) == 1)
  {
    sum += n;
    i++;        

    /**
     * We do not need to compute the average in the body of the loop;
     * that's something that only needs to be done after we've read
     * all the inputs.
     */
  }

  /**
   * Make sure we exited the loop normally by checking that items_read is
   * not 0 and that we don't have a read error on the input stream:
   */
  if ( items_read == 0 || ferror( fin ) )
  {
    fputs( "Badly formed input file or error on input - exiting", stderr );
    fclose( fin );
    exit( EXIT_FAILURE );
  }

  /**
   * Otherwise we've read to the end of the file.  
   *
   * It's a good idea to free resources (file pointers, dynamically allocated
   * memory, etc.) as soon as you're done with them, rather than waiting
   * until the end of the program, so we'll close the input file here. 
   */
  fclose(fin); 

  /**
   * *Now* we compute the average.
   */ 
  avg = (sum / i);

  printf("The average is %d.\n", avg);


  return EXIT_SUCCESS;
}

  • 标识符(变量名、函数名、枚举常量等)中唯一可以出现的标点符号是下划线-
  • 除非您确实受到空间限制,或者您特别不希望达到该精度级别,否则请使用
    double
    而不是
    float
    。大多数情况下,
    float
    值都隐式提升为
    double

    请编辑您的代码,使其更具可读性,不要有一半的行是空白的,并且有适当的缩进,而不是
    文件*numbers.txt(语法无效)?如何运行此代码?它甚至不应该编译(
    numbers.txt
    不是有效的标识符)。您确定这正是您正在编译和运行的代码吗?
    double sum = 0.0, n = 0.0, avg = 0.0;
    ...
    while( (items_read = fscanf( fin, "%lf", &n )) == 1 )
    ...
    printf( "The average is %f\n", avg );