title: Can not read all numbers from a txt file in C
body: <p>I have a program that runs in MPI. Though the actual problem is just a part of the program where Processor 0 reads from a txt file some numbers into a dynamic array. To identify that I scan to the end of the file, I'm using the file pointer and divide the result with 4 (which is the number of bytes every int needs) and should have the right size to allocate the memory. Problem is, that it does not work as intented and end up with half the size so I will read half the file and the program will not be correct.</p>
  
  <p>I have tried to format the numbers in the file alternatively assuming that it was a problem of this nature but nothing.</p>
  
  <p>My Data.txt:
  <code>1 2 3 4 5 6 7 8 9 10 11 12</code></p>
  
  <p>The section of the code that reads from the file:</p>
  
  <pre><code>           fp = fopen("Data.txt", "r");
             if (fp == NULL) {
                  fprintf(stderr, "Error while opening file.\n");
                  MPI_Abort(newComm, 2);
              }
  
              fseek(fp, 0, SEEK_END);
              n = ftell(fp);
              n /= sizeof(int);
              printf("n = %d\n", n);
              fseek(fp, 0, SEEK_SET);
  
              workLoad = n / p;
  
              if (n % p != 0) {
                  fprintf(stderr, "The number of elements in the file MOD the number of proccessors must equal zero.\n");
                  MPI_Abort(newComm, 2);
              }
  
              arr = (int*)malloc(n * sizeof(int));
              if (arr == NULL) {
                  fprintf(stderr, "Error in malloc!\n");
                  MPI_Abort(newComm, 1);
              }
  
              for (i = 0; i &lt; n; i++)
                  fscanf(fp, "%d", arr + i);
  
              fclose(fp);
  
              printf("Numbers loaded from file.\n");
              break;
  </code></pre>
  
  <p>The expected output should be an array with 12 elements such as the the numbers of the file.</p>
  
  <p>The actual output is an array with 6 elements until number 6 so half the actual size.</p>