输入文本文件的结果int值不存储在数组中。C语言

输入文本文件的结果int值不存储在数组中。C语言,c,C,//结果值似乎没有存储在tab[]数组中。循环完成后,数组为空。 我希望它是一个动态数组,所以我不需要声明数组的具体大小。示例文本文件输入位于代码末尾。实际的文本输入大约有1000行。感谢你的帮助。谢谢// #include <stdio.h> #include <stdlib.h> void read_ints(const char* file_name, int *result); int main() { int result =0; read_i

//结果值似乎没有存储在tab[]数组中。循环完成后,数组为空。 我希望它是一个动态数组,所以我不需要声明数组的具体大小。示例文本文件输入位于代码末尾。实际的文本输入大约有1000行。感谢你的帮助。谢谢//

#include <stdio.h>
#include <stdlib.h>
void read_ints(const char* file_name, int *result);

int main()
{
    int result =0;
    read_ints("numbers.txt", &result);
}

void read_ints (const char* file_name, int *result)
{
  FILE* file = fopen ("numbers.txt", "r");
  int i = 0;
  int n=0; //row number//
  int m;
  int tab[m]; //array//
  if (file == NULL)
  {
   printf("unable to open file %s", file_name);
  }
  while ( fscanf (file, "%d", &i) ==1)
    {
       n++;
      printf ("%d\n ", i);
      *result += i;
      printf("\n we are at row nr. %d sum of this number and all numbers before is: %d\n", n, *result);
       tab[n]==*result;
    }
          printf("\nnumber from row number one is ... : %d\n", tab[1]); //this does not work properly //
  fclose (file);
}
这是未定义的行为。一个像样的编译器会对这件事有所了解。始终在运行的所有编译中启用所有警告,并将所有警告视为错误


没有自动增长的数组,也没有可以容纳任意数量元素的数组。您可以尽早决定大小并坚持使用,也可以使用动态数组(查找),根据需要显式手动调整大小。

int m;int tab[m]-所以。。。数组的大小是多少?至于你问的问题,你不给数组的任何元素赋值(考虑到@EugeneSh提到的大小问题,这很好。)。
=
不是赋值运算符。移动
n++可能会更好
到循环的末尾,并将
&&n
添加到
while
循环条件中。“我可以使用相同的数组声明显示代码,该数组声明可以100%工作”。不,你不能。在您测试的两个案例中,它可能是偶然起作用的,但它“100%”不起作用。
-14
+15
+9
+19
+18
+14
+14
-18
+15
+4
-18
-20
-2
+17
+16
-7
-3
+5
+1
-5
-11
-1
-6
-20
+1
+1
+4
+18
+5
-20
-10
+18
+5
-4
-5
-18
+9
+6
int m;
int tab[m];