0

I am not sure where I am going wrong. The file I am trying to read:

1 1 1 1 1 1 1 2 2 2 
2 3 3 3 3 4 4 5 5 5 
5 5 5 5 5 6 6 6 7 7 
7 8 8 8 9 9 9 9 9 9 
10 10 11 11 11 12 12 12 12 12 
12 13 13 13 14 14 14 15 15 15 
15 15 15 16 16 16 16 17 18 18 
18 18 18 19 19 19 19 19 19 20 
20 20 20 20 20 21 21 21 21 21 
22 22 23 23 24 25 25 25 26 26 

This is my code:

#include<stdio.h>
using namespace std;
int main(){
    FILE *fptr;
    char FILENAME[100];
    printf("Enter the name of the file:");
    gets(FILENAME);
    fptr=fopen(FILENAME,"r");
    if(fptr==NULL){
        printf("File could not be opened");
    }
    int count=0,space=0;

    while ((count = fgetc(fptr)) != EOF)
        {
            if (count == ' ')
                space++;

        }
       printf("Number of integers in file: %d \n\n",space);  
    int storedData[space];
    for(int i=0;i<space;i++){
          fscanf(fptr,"%d", &storedData[i]);
          
    }
    for(int i=0;i<space;i++){
        printf("%d \n", storedData[i]);  
    }
       
    return 0;
} ```

**Output**
Enter the name of the file:Listofnumbers.dat
Number of integers in file: 100 

0
0
0
0
-810195648
22737
-83690506
32763
10904080
0
34
0
-83207872
32763
56
0
0
0
0
0
34
0
-83690617
32763
1
0
0
0
1
0
0
0
0
0
-83696309
32763
0
0
0
0
8
0
16
0
34
0
-83529122
32763
1
0
-83232208
32763
0
0
-810189776
22737
34
0
-83479343
32763
-83232208
32763
-83232208
32763
6421816
0
-83232208
32763
34
0
-83522581
32763
6421808
0
-83232208
32763
1
0
-83232208
32763
-83232112
32763
70
0
10884048
0
400
0
4199998
0
4210744
0
100
0
-83207872
32763
6421512
0
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Infinitypool
  • 1
  • 1
  • 2
  • 2
    The `using namespace std;` is C++, not C. Your code should not compile. – Basile Starynkevitch Apr 22 '21 at 05:11
  • And `gets` is dangerous (buffer overflow possible) and obsolete. Check by reading [this C reference](https://en.cppreference.com/w/c) then a good C standard like [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) – Basile Starynkevitch Apr 22 '21 at 05:13
  • If allowed, compile your C code with a recent [GCC](http://gcc.gnu.org/) compiler used as `gcc -Wall -Wextra -g`. Don't forget to **read the documentation of your compiler.** Then use a debugger, e.g. [GDB](https://www.gnu.org/software/gdb/). Of course, **read the documentation of your debugger.** – Basile Starynkevitch Apr 22 '21 at 05:14
  • 1
    You're printing uninitialized memory. Check the return value of `fscanf` and if it indicates failure, don't go on to print out that location that failed to be read into – M.M Apr 22 '21 at 05:16
  • Thanks for the help @Basile Starynkevitch, It works now after rewinding the file. But whenever there is a newline, a lot of zeroes are displayed and I'm not sure why. I cant use %c because I need to store the numbers in a 10 by 10 array. – Infinitypool Apr 22 '21 at 15:02

1 Answers1

2

Check the return value from fscanf().

Rewind the file before trying to reread it.

Never use gets(), either — it is far too dangerous.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • It works now after rewinding the file. But whenever there is a newline, a lot of zeroes are displayed and I'm not sure why. I cant use %c because I need to store the numbers in a 10 by 10 array. – Infinitypool Apr 22 '21 at 15:04
  • You're counting spaces between integers; you probably need to count newlines too. What value are you getting for `space`? If you get 90 (9 space separators on each of 10 lines), you aren't allocating enough space. You should also allow for multiple consecutive white space characters, though those would only increase the space used. You might need to count the actual number of numbers read. If you know about dynamic memory allocation, you could use `malloc()` et al to allocate space. If the files will always contain just 100 numbers, you could simply allocate an array of 100 integers. – Jonathan Leffler Apr 22 '21 at 15:33