0

It seems like I didn't quite understand how the file stream works. My text file right now contains the following integers: 1 10 5 4 2 3 -6, but I would like the program to be able to read any number of integers from the file, should it change.

Apparently I'm not even using the correct functions. The code I have written is the following:

 int main() {
     printf("This program stores numbers from numeri.txt into an array.\n\n");
     int a[100];
     int num;
     int count = 0;

     FILE* numeri = fopen("numeri.txt", "r");

     while (!feof(numeri)) {
         num = getw(numeri);
         a[count] = num;
         if (fgetc(numeri) != ' ')
             count++;
     }

     int i;
     for (i = 0; i < count; i++) { printf("%d ", a[i]); }

     return 0;
}

I would like it to print out the array with the stored numbers, but all I get is: 540287029 757084960 -1

Can someone help me understand what I did wrong and maybe tell me how to write this kind of code properly?

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
  • 1
    What is `getw`? What does it do? – Some programmer dude Sep 06 '19 at 13:42
  • 2
    Also please read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Some programmer dude Sep 06 '19 at 13:43
  • 2
    man `putw/getw` : getw() reads a word (that is, an int) from stream. It's provided for compatibility with SVr4. We **recommend you use fread(3)** instead. putw() writes the word w (that is, an int) to stream. It is provided for compatibility with SVr4, but we **recommend you use fwrite(3)** instead. – chux - Reinstate Monica Sep 06 '19 at 13:48
  • 2
    @chux Ah, that explains the problem. The OP is reading a *text* file, and `getw` read as if from a *binary* file. So the simple solution is probably to use [`fscanf`](https://en.cppreference.com/w/c/io/fscanf) (and remember to check [*what it returns*](https://en.cppreference.com/w/c/io/fscanf#Return_value), as part of the loop condition). – Some programmer dude Sep 06 '19 at 13:52
  • 2
    I'm spotting a potential buffer overflow due to `count` can become higher than 100, which is the size of your array `a`. This results in an undefined behaviour. – DrosvarG Sep 06 '19 at 13:58

1 Answers1

2

I have fixed your code based on comments. I used fscanf to read from file and limited the amount of numbers that can be stored in array by checking count < 100 and checking whether fscanf filled exactly one argument.

Also, I checked that whether file could be opened or not, just for safety. If it couldn't be opened, then just print error message and return 1.

int main() {

    printf("This program stores numbers from numeri.txt into an array.\n\n");
    int a[100] = {0};
    int num;
    int count = 0;
    int i = 0;

    FILE* numeri = fopen("numeri.txt", "r");
    if (numeri == NULL) {
        printf("Can't open file\n");
        return 1;
    }

    while (count < 100 && fscanf(numeri, "%d", &num) == 1) {
       a[count++] = num;
    }

    for (i = 0; i < count; i++) { printf("%d ", a[i]); }

    return 0;
}
Community
  • 1
  • 1
kiner_shah
  • 3,939
  • 7
  • 23
  • 37