0

So I need output size of array in this file text and to do this I must break the loop in the last position by using NULL to break but the problem here that when arr[i] come to value 0, it equal to NULL and break at that position so my size of array is not complete. How to resolve it?
Thanks for support!
The file .txt input:

3
4
0
5
6


The code:

#include <stdio.h>

int main() {
   char a[20];
   char e[40];
   int arr[30];
   int num, key, k = 0, len = 0;

   printf("Enter a filename: ");
   scanf("%s", &a);
   scanf("%c", &e);
   
   FILE* rfile;
   rfile = fopen(a, "r");
   if (rfile == NULL) {
       printf("Not found the file !!!");
   }
   else {
       printf("Successfully accessed the file: %s\n", a);
       int i;
       for (i = 0; i < 30; i++) {
           fscanf(rfile, "%d", &arr[i]);
           fscanf(rfile, "%c", &e);
           if (arr[i] == NULL) {      // PROBLEM HERE
               break;
           }
           len++;
       }
   }
   printf("The size of array: %d", len);
   return 0;
}
Znone
  • 51
  • 4

2 Answers2

3

You can find some more details regarding what NULL is here, but you should save NULL for pointer comparisons, not comparing against ints as you are doing. In fact, your usage generates a warning:

warning: comparison between pointer and integer

Despite that, 0 == NULL will evaluate to true. Since 0 is in your list of values, you prematurely break revealing your problem. Instead, you simply need to read the entire file, either until you run out of room in your array (already covered by your for loop) or reach the end of the file (designated by EOF). To determine that, you need to check the return value of fscanf. Below is an example of a possible implementation:

#include <stdio.h>
#include <stdlib.h>

int main() {
   int arr[30];
   int len = 0;

   FILE* rfile;
   rfile = fopen("file.txt", "r");
   if (rfile == NULL) {
       printf("Not found the file !!!");
       exit(-1);
   }
   else {
       int i;
       for (i = 0; i < 30; i++) {
           // fscanf returns the number of correctly matched items, or
           // EOF when the end of the file is reached (or EOF on error)
           int ret = fscanf(rfile, "%d", &arr[i]);
           // did we get a correct match?
           if (ret == 1)
           {
             // we matched one number as expected, increment len
             len++;
           }
           // did we reach the end of file?
           else if (ret == EOF)
           {
             // EOF can also indicate an error, check errno here to determine if
             // an error occurred instead of end of file, if you want
             break;
           }
       }
   }
   // prints 5 with your input file example
   printf("The size of array: %d\n", len);
   return 0;
}

I have no idea what you were trying to accomplish with e, so I removed that as well as other unused variables, and hardcoded user input.

yano
  • 4,827
  • 2
  • 23
  • 35
  • 1
    OP was probably using `scanf("%c", &e);` to discard the newline character from the input stream. However, you are right that in this case, it is not necessary, as it will automatically be consumed by the `%d` `scanf` conversion format specifier. – Andreas Wenzel Jan 03 '22 at 21:48
  • @AndreasWenzel I think you're right, but an array of 40 is what had me truly perplexed. – yano Jan 04 '22 at 01:15
0

arr is the array of ints arr[i] has type int. NULL is a pointer.

If 0 indicated the end of the data (sentinel value) then:

if (arr[i] == 0) break;

or in a short form

if (!arr[i]) break;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0___________
  • 60,014
  • 4
  • 34
  • 74