-1

// the result values doesnt seem to store in the tab[] array. after the loop is finished the array is empty. i intented it to be a dynamic array, so that i dont need to declare the specific size of the array. the example text file input is at the end of the code. The actual text input has about 1000 rows. Help apreciated. Thanks//

#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);
}

numbers.txt:

-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
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • 3
    `int m; int tab[m];` - so... what size will the array be? – Eugene Sh. Jan 08 '19 at 20:04
  • As for the problem you ask about, you don't assign anything to any elements of the array (which is pretty good, considering the size-issue mentioned by @EugeneSh.). – Some programmer dude Jan 08 '19 at 20:05
  • Lastly, don't forget that array indexes are *zero* based, so the first element starts at index `0`. – Some programmer dude Jan 08 '19 at 20:06
  • 2
    `==` is *not* an assignment operator. – Eugene Sh. Jan 08 '19 at 20:06
  • 1
    You might do better moving `n++;` to the end of the loop, and adding `&& n < m` to the `while` loop condition. – Weather Vane Jan 08 '19 at 20:06
  • @Eugene Sh. as i stated, i intented not to decide on the size of the array when declaring the array. i used such a statement before, i can show the code with same array declaration that works 100 % –  Jan 08 '19 at 20:23
  • @Some programmer dude "tab[n]==*result;" <== here is the assignment of the result to particular part of the array. i know that the arrays are 0 based, i was to fix that but i first would like the main part of code to work properly –  Jan 08 '19 at 20:24
  • This is not an assignment. Assignments are done using `=`, not `==`. What you do is comparing an element that may or may not be inside the bounds of the array with a sum of numbers. That does not assign anything. – Gerhardh Jan 08 '19 at 20:26
  • @Eugene Sh. i changed "==" to "=" , but now the code works only to 3rd loop, and after it stops, and i have 1000 integers to calc –  Jan 08 '19 at 20:28
  • The compiler does not care whether you want to decide on the size of the array. The variable `m` has some unspecified value at this line. Whatever value this is, is used as size of the array. You cannot decide differently later. – Gerhardh Jan 08 '19 at 20:28
  • @Gerhardh ok, i changed "==" to "=", but it only comutes 3 times and it stopes. btw can i edit the code in the main post save edits somewhere else on stackoverflow ? –  Jan 08 '19 at 20:29
  • Besides fixing the size of the array, you should provide part of the content of the file and output you get. – Gerhardh Jan 08 '19 at 20:31
  • @Gerhardh do you mean post the text file with the numbers ? how should i do it –  Jan 08 '19 at 20:39
  • Yes, at least the first few lines. Just add it as code – Gerhardh Jan 08 '19 at 20:52
  • @Gerhardh ok, so i just edit the original post, right? –  Jan 08 '19 at 20:56
  • Yes. Edit the post to add input and output – Gerhardh Jan 08 '19 at 21:05
  • #Gerhardh ok, thx. I edited the post and included part of the text input. –  Jan 08 '19 at 21:21
  • Did you change the broken array definition? If I run your code with updated assignment, it causes segmentation fault. Is this what you mean by "it stops"? – Gerhardh Jan 09 '19 at 09:40
  • 1
    "i can show the code with same array declaration that works 100 %". No you cannot. It might have worked by accident for the couple of cases you have tested, but it does not work "100%". – n. m. could be an AI Jan 09 '19 at 09:45
  • Please update the question and add the modified code. Without this we cannot judge which errors that we told you, were already fixed and which not. – Gerhardh Jan 09 '19 at 11:45

1 Answers1

1
int m;
int tab[m]; 

This is undefined behaviour. A decent compiler would have warned you about this. Always enable all warnings in all compilations you run, and treat all warnings as errors.

There is no such thing as array that automatically grows, or an array that can accommodate any number of elements. You can either decide on the size early on and stick with it, or use a dynamic array (look it up) and explicitly and manually resize it as needed.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243